GeoDjango does not change field type in PostGis to geography if geographic=True - possible bug

21 views
Skip to first unread message

Piotr Kowenzowski

unread,
Jul 23, 2016, 9:33:34 PM7/23/16
to Django users
Hi,
I had problem with the query:
JobOffer.objects.filter(
 location__distance_lte
=(
 current_location
,
 D
(km=radius))
)

because it returned everything.

when I looked into SQL:

SELECT
"joboffers_joboffer"."id", "joboffers_joboffer"."owner_name", "joboffers_joboffer"."title", "joboffers_joboffer"."description", "joboffers_joboffer"."date", "joboffers_joboffer"."starts", "joboffers_joboffer"."salary_m", "joboffers_joboffer"."salary_h", "joboffers_joboffer"."category", "joboffers_joboffer"."phone_number", "joboffers_joboffer"."email", "joboffers_joboffer"."location" FROM "joboffers_joboffer" WHERE ST_Distance("joboffers_joboffer"."location", ST_GeomFromEWKB('\001\001\000\000 \346\020\000\000\000\000\000\000\000\000I@\000\000\000\000\000\000I@'::bytea)) <= 1000.0

I looked into database and found that location is Geometry.

I looked into documentation for PostGIS and found that ST_Distance:

For geometry type returns the minimum 2D Cartesian distance between two geometries in projected units (spatial ref units). For geography type defaults to return the minimum geodesic distance between two geographies in meters. If use_spheroid is false, a faster sphere calculation is used instead of a spheroid.

So I removed  geographic=True and get query:
SELECT "joboffers_joboffer"."id", "joboffers_joboffer"."owner_name", "joboffers_joboffer"."title", "joboffers_joboffer"."description", "joboffers_joboffer"."date", "joboffers_joboffer"."starts", "joboffers_joboffer"."salary_m", "joboffers_joboffer"."salary_h", "joboffers_joboffer"."category", "joboffers_joboffer"."phone_number", "joboffers_joboffer"."email", "joboffers_joboffer"."location" FROM "joboffers_joboffer" WHERE ST_Distance_Sphere("joboffers_joboffer"."location", ST_GeomFromEWKB('\001\001\000\000 \346\020\000\000\000\000\000\000\000\000I@\000\000\000\000\000\000I@'::bytea)) <= 1000.0
and everything started working fine.

From django documenation:
The geography type provides native support for spatial features represented with geographic coordinates (e.g., WGS84 longitude/latitude).[6] Unlike the plane used by a geometry type, the geography type uses a spherical representation of its data. Distance and measurement operations performed on a geography column automatically employ great circle arc calculations and return linear units. In other words, when ST_Distance is called on two geographies, a value in meters is returned (as opposed to degrees if called on a geometry column in WGS84).

BUT POINT FIELD IS STILL GEOMETRY INSTEAD OF GEOGRAPHY


Reply all
Reply to author
Forward
0 new messages