简体   繁体   English

postgis 如何处理使用不同 SRID 发送的坐标

[英]How is postgis treating coordinates sent with different SRID

I am running a django application and I am using the PostGis extension for my db.我正在运行 django 应用程序,并且我正在为我的数据库使用 PostGis 扩展。 I am trying to understand better what happens under the hood when I send coordinates, especially because I am working with different coordinate systems which translate to different SRIDs.我试图更好地理解当我发送坐标时会发生什么,特别是因为我正在使用不同的坐标系,这些坐标系转换为不同的 SRID。 My question is threefold:我的问题有三个:

  1. Is django/postgis handling the transformation when creating a Point or Polygon in the DB.在数据库中创建点或多边形时,django/postgis 是否处理转换。
  2. Can I query it back using a different SRID我可以使用不同的 SRID 查询它吗
  3. Is it advisable to use the default SRID=4326是否建议使用默认的 SRID=4326

Let's say I have a model like this (note I am setting the standard SRID=4326):假设我有一个像这样的 model(注意我正在设置标准 SRID=4326):

class MyModel(models.Model):

    name = models.CharField(
        max_length=120,
    )
    point = models.PointField(
        srid=4326,
    )
    polygon = models.PolygonField(
        srid=4326,
    )
  1. Now I am sending different coordinates and polygons with different SRIDS.现在我正在发送具有不同 SRIDS 的不同坐标和多边形。

I am reading here in the django docs that:我在 django 文档中阅读:

Moreover, if the GEOSGeometry is in a different coordinate system (has a different SRID value) than that of the field, then it will be implicitly transformed into the SRID of the model's field, using the spatial database's transform procedure此外,如果 GEOSGeometry 位于与字段不同的坐标系中(具有不同的 SRID 值),则使用空间数据库的转换过程将其隐式转换为模型字段的 SRID

So if I understand this correctly, this mean that when I am sending an API request like this:因此,如果我理解正确,这意味着当我发送这样的 API 请求时:

data = {
   "name": "name"
   "point": "SRID=2345;POLYGON ((12.223242267 280.123144553))"
   "polygon": "SRID=5432;POLYGON ((133.2345662 214.1429138285, 123.324244572 173.755820912250072))"

}

response = requests.request("post", url=url, data=data)

Both - the polygon and the point - will correctly be transformed into SRID=4326??多边形和点都将正确转换为 SRID=4326??

EDIT:编辑:

When I send a point with SRID=25832;POINT (11.061859 49.460983) I get 'SRID=4326;POINT (11.061859 49.460983)' from the DB.当我发送一个SRID=25832;POINT (11.061859 49.460983)的点时,我从数据库中得到'SRID=4326;POINT (11.061859 49.460983)' When I send a polygon with 'SRID=25832;POLYGON ((123.2796155732267 284.1831980485285, ' '127.9249715130572 273.7782091450072, 142.2351651215613 ' '280.3825718937042, 137.558146278483 290.279508688337, ' '123.2796155732267 284.1831980485285))' I get a polygon 'SRID=4326;POLYGON ((4.512360573651161 0.002563158966576373, ' '4.512402191765552 0.002469312460126783, 4.512530396754145 ' '0.002528880231016955, 4.512488494972807 0.00261814442892858, ' '4.512360573651161 0.002563158966576373))' from the DB When I send a polygon with 'SRID=25832;POLYGON ((123.2796155732267 284.1831980485285, ' '127.9249715130572 273.7782091450072, 142.2351651215613 ' '280.3825718937042, 137.558146278483 290.279508688337, ' '123.2796155732267 284.1831980485285))' I get a polygon 'SRID=4326;POLYGON ((4.512360573651161 0.002563158966576373, ' '4.512402191765552 0.002469312460126783, 4.512530396754145 ' '0.002528880231016955, 4.512488494972807 0.00261814442892858, ' '4.512360573651161 0.002563158966576373))' from the DB

  1. Can I query it back using a different SRID我可以使用不同的 SRID 查询它吗

Unfortunately I haven't found a way to query the same points back to their original SRID.不幸的是,我还没有找到将相同点查询回其原始 SRID 的方法。 Is this even possible?这甚至可能吗?

  1. And lastly I am working mostly with coordinates in Europe but I also might have to include sporadically coordinates from all over the world too.最后,我主要使用欧洲的坐标,但我也可能不得不偶尔包含来自世界各地的坐标。 Is SRID=4326 a good standard to use? SRID=4326是一个很好的标准吗?

Thanks a lot for all the help in advance.非常感谢您提前提供的所有帮助。 Really appreciated.非常感谢。

Transforming SRS of geometries is much more than just changing their SRID.转换几何的 SRS 不仅仅是改变它们的 SRID。 So, if for some reason after a transformation the coordinates return with exactly the same values, there was most probably no transformation at all.因此,如果由于某种原因在变换后坐标返回完全相同的值,则很可能根本没有变换。

This example uses ST_Transform to transform a geometry from 25832 to 4326 .此示例使用ST_Transform将几何图形从25832转换为4326 See the results yourself:自己查看结果:

WITH j (geom) AS (
 VALUES('SRID=25832;POINT (11.061 49.463)'::geometry))
SELECT ST_AsEWKT(geom),ST_AsEWKT(ST_Transform(geom,4326)) FROM j;

 

       st_asewkt            |                      st_asewkt                       
---------------------------------+------------------------------------------------------
 SRID=25832;POINT(11.061 49.463) | SRID=4326;POINT(4.511355210946569 0.000446125446657)
(1 Zeile)
  • The Polygon transformation in your question is btw correct.顺便说一句,您问题中的多边形转换是正确的。

Make sure that django is really storing the values you mentioned.确保 django 确实存储了您提到的值。 Send a 25832 geometry and directly check the SRS in the database.发送25832几何,直接查看数据库中的SRS。 If you're only checking using django, it might be that it is transforming the coordinates back again in the requests, which might explain you not seeing any difference.如果您仅使用 django 进行检查,则可能是它在请求中再次将坐标转换回,这可能说明您没有看到任何差异。

To your question:对于你的问题:

Is SRID=4326 a good standard to use? SRID=4326 是一个很好的标准吗?

WGS84 is the most used SRS worldwide, so I'd tend to say yes, but it all depends on your use case. WGS84 是全球使用最多的 SRS,所以我倾向于说是的,但这完全取决于您的用例。 If you're uncertain of which SRS to use, it might indicate that your use case does not impose any constraint to it.如果您不确定要使用哪个 SRS,则可能表明您的用例没有对其施加任何约束。 So, stick to WGS84 but keep in mind that you don't mix different SRS in your application.因此,请坚持使用 WGS84,但请记住不要在应用程序中混合使用不同的 SRS。 Btw: if you try to store geometries in multiple SRS in the same table, PostgreSQL will raise an exception;)顺便说一句:如果您尝试将几何图形存储在同一张表中的多个 SRS 中,PostgreSQL 将引发异常;)

Further reading: ST_AsEWKT , WGS84进一步阅读: ST_AsEWKTWGS84

First of all, I'm not big expert at GIS (I have created just a few small things in Django and GIS), but... In this documentaion about GeoDjango: https://docs.djangoproject.com/en/3.1/ref/contrib/gis/tutorial/#automatic-spatial-transformations .首先,我不是 GIS 方面的专家(我在Django和 GIS 中只创建了一些小东西),但是... /ref/contrib/gis/tutorial/#automatic-spatial-transformations According to it:据它说:

When doing spatial queries, GeoDjango automatically transforms geometries if they're in a different coordinate system.在进行空间查询时,如果几何图形位于不同的坐标系中,GeoDjango 会自动转换它们。 ... ...

Try in console ( ./manage.py shell ):在控制台中尝试( ./manage.py shell ):

from <yourapp>.models import MyModel
obj1 = MyModel.objects.all().first()
print(obj1)
print(obj1.point)
print(dir(obj1.point))
print(obj1.point.srid)

--edit-- You can manually test converting between SRID similary to this page: https://gis.stackexchange.com/questions/94640/geodjango-transform-not-working --edit-- 您可以手动测试与此页面类似的 SRID 之间的转换: https://gis.stackexchange.com/questions/94640/geodjango-transform-not-working

obj1.point.transform(<new-srid>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM