I have a polygon in my model citys but in my map for example
bogota has the coordinate -8243997.66798 , 517864.86656
-> open street maps; but i need make query with coordinates like (4.697857, -74.144554)
-> google maps.
pnt = 'POINT(%d %d)'%(lon, lat)
zonas = ciudad.zona_set.filter(polygon__contains=pnt)
zonas is empty :/ ,
how i can convert lat
and lon
to standar coordinates in open street map , or how know the srid code for one input (lat,lon)
pnt = GEOSGeometry('POINT(-96.876369 29.905320)', srid=srid_from_lat_lon)
thanks
When making spatial queries, its good practice to pass a geometry that has a specified spatial reference system (srid). Like this, GeoDjango will automatically convert the input query geometry to the coordinate system of your table (the coordinate systems of your city model in your case).
In the first code example you gave, you do not specify an srid on the geometry, so
pnt = 'POINT(%d %d)'%(lon, lat)
does not have an srid. In this case, GeoDjango will assume the srid is the same for the input and the model data table. Which is not the case in your example, and that is why you dont get any matches.So you will need to create you point with the correct SRID. If you get the coordinates from OSM, most likely the coordinates are in the Web Mercator projection, which has the srid
3857
. This projection is often used in web mapping.For this, you can use the EWKT format (which is essentially SRID + WKT) like so:
Or if you have the coordinates in Web Mercator Projection, the following should work:
Just for reference, here are a few examples on how to go back an forth between EWKT and GEOSGeometries:
So this (normal WKT, with srid specified on creation of geometry)
is equivalent to this (srid contained in EWKT string):