How do I convert a latitude/longitude pair into a PostGIS geography type?

前端 未结 3 1961
我寻月下人不归
我寻月下人不归 2021-01-30 17:27

I\'m trying to load a bunch of latitude/longitude pairs into a PostGIS geography type so as to be able to query by location.

In particular I have a table with float lat

相关标签:
3条回答
  • 2021-01-30 17:50

    Here are some different ways to make geography types:

    • Convert numeric long and lat columns to a geog geography type:

       UPDATE mytable SET geog = ST_SetSRID(ST_MakePoint(long, lat), 4326)::geography
      
    • Convert a geom geometry column (SRID=4326) to a geog geography type using a simple cast:

       UPDATE mytable SET geog = geom::geography
      
    • Transform a projected geom geometry column to a geog geography type:

       UPDATE mytable SET geog = ST_Transform(geom, 4326)::geography
      

    Note that the last two examples work on any geometry type. Also, the conversion from geometry to geography is often implicit, and these examples work without ::geography, however explicit casts are usually a good practice for these things.

    0 讨论(0)
  • 2021-01-30 18:00

    To perform exchange between lat and lng you may use:

    update mytable set geography = ST_GeographyFromText('SRID=4326;POINT(' || st_x(geom) || ' ' ||  st_y(geom) || ')');
    

    with or without srid.

    0 讨论(0)
  • 2021-01-30 18:01

    ...sigh. Stupidity on my part. Apparently the correct order is longitude, latitude. I was fooled into thinking that both coordinates had the same range (-180 to 180) so thought something more subtle was going on.

    0 讨论(0)
提交回复
热议问题