I have a table with over 800,000 records including a lat and long column. The data source did not create geometries, so I was looking around how to do this. I know there's ST_MakePoint(x,y,SRID) but all the documentation and other threads show how to create single points. How do I create the geometry type using these columns?
-
1do you mean create lines from these points? Because points are geometry just as lines and polygons are... So if you're looking at creating lines from these points, check this recent blog post from Paul Ramsey: blog.cleverelephant.ca/2015/03/making-lines-from-points.htmlInactivated Account– Inactivated Account2015-05-04 16:29:38 +00:00Commented May 4, 2015 at 16:29
-
Nope. I meant like converting them to geometry types to be used in spatial queries. Looks like my question has been answered though so thank you!dknuth– dknuth2015-05-04 21:00:19 +00:00Commented May 4, 2015 at 21:00
5 Answers
Newer versions of PostGIS allow the following, slightly more common, syntax:
ALTER TABLE your_table ADD COLUMN geom geometry(Point, 4326);
Then use ST_SetSrid and ST_MakePoint to populate the column:
UPDATE your_table SET geom = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326);
See documentation here:
-
3You can also use the newer
ST_POINT(lon, lat, srid)function. The srid parameter is optional, and you can pass this in theST_SetSRIDinstead, like above.Theo F– Theo F2023-06-23 17:54:09 +00:00Commented Jun 23, 2023 at 17:54
If you haven't already add a geometry column (assuming EPSG:4326 here):
SELECT AddGeometryColumn ('your_table','geom',4326,'POINT',2);
Then UPDATE, passing in the names of your x and y fields:
UPDATE yourtable SET geom = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326);
This will update the entire table, unless you include a WHERE clause
-
7I think ST_SRID should be ST_SetSRID. I got an error when using ST_SRIDVipin Gupta– Vipin Gupta2019-05-17 11:48:46 +00:00Commented May 17, 2019 at 11:48
As a point of clarity for future users, lat / long coordinates are technically Geography (not Geometry). It is better convention to name geometry/geography database fields in accordance with the type of the object. Storing what is technically Geography in a column called 'geom' does not allow you to glance at the field and know what is actually there.
Data in lat / long are geography (i.e., geographic coordinate system) SRID: 4326
Data in a projected coordinate system like UTM, Web Mercator, State Plane, etc. are geometry (i.e., Cartesian coordinates).
Reference: http://postgis.net/workshops/postgis-intro/geography.html
-
1Many spatial functions accept only geometry, not geography, some accept both. Therefore I prefer to use geometry over geography.anneb– anneb2022-04-13 21:32:52 +00:00Commented Apr 13, 2022 at 21:32
Add a geom column to your table
ALTER TABLE tableName ADD COLUMN geom geometry(Point, 4326);Update the geom column using the below command
UPDATE tableName SET geom = ST_SetSRID(ST_MakePoint(longitude::double precision, latitude::double precision), 4326);
I have this solution to add geom column of WSGI and UTM srid values
Synopsis geometry ST_MakePoint(float x, float y); geometry ST_MakePoint(float x, float y, float z); geometry ST_MakePoint(float x, float y, float z, float m);
Note:- For geodetic coordinates, X is longitude and Y is latitude
lets add geom(geometry column)
SELECT AddGeometryColumn ('group_data','common_datatable','geom',4326,'Point',2);
Now we will update our geom column values from our lat, long column
UPDATE group_data.common_datatable
SET geom = ST_SetSRID(
ST_MakePoint(longitude, latitude)
,4326);
lets create spatial index also for faster spatial query
-- DROP INDEX IF EXISTS group_data.sidx_common_datatable_geom;
CREATE INDEX IF NOT EXISTS sidx_common_datatable_geom
ON group_data.common_datatable USING gist
(geom);
To create geomtery column in UTM co-ordinate system also
-- Adding geom_utm column for utm srid value
SELECT AddGeometryColumn ('group_data','common_datatable','geom_utm',32643,'Point',2);
UPDATE group_data.common_datatable
SET geom_utm = ST_Transform(geom, 32643)
FROM spatial_ref_sys
WHERE ST_SRID(geom) = srid;
-- DROP INDEX IF EXISTS group_data.sidx_common_datatable_geom_utm;
CREATE INDEX IF NOT EXISTS sidx_common_datatable_geom_utm
ON group_data.common_datatable USING gist
(geom_utm);