1

我在我的模型城市中有一个多边形,但在我的地图中,例如波哥大有坐标-8243997.66798 , 517864.86656-> 打开街道地图;但我需要使用坐标进行查询,例如(4.697857, -74.144554)-> 谷歌地图。

pnt = 'POINT(%d %d)'%(lon, lat)
zonas = ciudad.zona_set.filter(polygon__contains=pnt)

zonas 是空的:/,我如何在开放的街道地图中转换lat和转换lon为标准坐标,或者如何知道一个输入的srid代码(lat,lon)

pnt = GEOSGeometry('POINT(-96.876369 29.905320)', srid=srid_from_lat_lon)

谢谢

4

1 回答 1

1

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:

pnt = 'SRID=4326;POINT(-96.876369 29.90532)'

Or if you have the coordinates in Web Mercator Projection, the following should work:

pnt = 'SRID=3857;POINT(-8243997.66798  517864.86656)'
zonas = ciudad.zona_set.filter(polygon__contains=pnt)

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)

GEOSGeometry('POINT(-8243997.66798  517864.86656)', srid=3857)

is equivalent to this (srid contained in EWKT string):

GEOSGeometry('SRID=3857;POINT(-8243997.66798  517864.86656)')
于 2017-08-30T10:59:31.767 回答