1

Background

I have a table hospitals_healthcenters_jointed_to_evacuationzones with following attributes:

  • lat latitude
  • long_ longitude

What I tried

I am trying to create a geometry datatype column Point that combines lat and long_

I used the following code that I found:

ALTER TABLE hospitals_healthcenters_jointed_to_evacuationzones
  ADD       shape Point
  ;

UPDATE  hospitals_healthcenters_jointed_to_evacuationzones
  SET   shape = Point("long_", "lat")

Error I got

But I get the following error:

ERROR:  function point(character varying, numeric) does not exist
LINE 4: SET     shape = Point("long_", "lat");
                        ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 157
alroc
  • 27,574
  • 6
  • 51
  • 97
Alex
  • 35
  • 2
  • 4
  • Hi Alex, welcome to SO! Please **mention the blog (link)** where you got your code from! This helps us to analyze your issue. TIP: use _backticks_ (`) or **CTRL + K** to format your code. The simple quote (') doesn't work. – hc_dev Nov 30 '19 at 20:01
  • By the way: each question on SQL should also mention the database/[DBMS](https://en.wikipedia.org/wiki/Database#Database_management_system) used, since functions and even SQL-syntax can differ This could be done via tags, e.g. [tag:sql-server] or [tag:oracle], etc. – hc_dev Nov 30 '19 at 20:05
  • Seems you're using [tag:postgresql]. Did you search for the _ERROR message_ or _SQL state_? Found this [question for SQL state and point](https://stackoverflow.com/questions/28717373/postgresql-st-geomfromtextunknown-does-not-exist). – hc_dev Nov 30 '19 at 20:15

2 Answers2

3

Assuming you are using PostgreSQL

Using PostGIS extension

With extension PostGIS installed there is the geography datatype for points with latitude and longitude.

To add a column:

ALTER TABLE hospitals_healthcenters_jointed_to_evacuationzones
  ADD COLUMN geo_point GEOGRAPHY(POINT,4326);

To update column geo_point for each record (based on existing coordinates in columns long, lat_):

UPDATE hospitals_healthcenters_jointed_to_evacuationzones
  SET geo_point = 'SRID=4326;POINT(' || long || ' ' || lat_ || ')';

The data value of point is given as text, see following example and explanation:

'SRID=4326;POINT(-71.104 42.315)'

  • Spatial Referencing System ID (SRID) is specified as 4326 for WGS 84 (same as used by GPS, see Wikipedia)
  • the geo-coordinates are specified as type POINT described by -71.104 42.315 in exactly this order: longitude, followed by space as separator, followed by latitude

Using native data types

Without PostGIS installed, you still have the geometric type point. Values of this type are constructed using the geometric conversion function point(double precision, double precision).

To add a column:

ALTER TABLE hospitals_healthcenters_jointed_to_evacuationzones
  ADD COLUMN geo_point POINT;

To update points for each record (based on existing coordinates in double typed columns long, lat_):

UPDATE hospitals_healthcenters_jointed_to_evacuationzones
  SET geo_point = point(long, lat_);

This (native) geometric point can later be converted to a geographic point if PostGIS is installed and should be used.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • Glad if I could help. Let me know, if the solution works (e.g. accept the answer). – hc_dev Dec 01 '19 at 00:12
  • I really appreciate it. This is the post that I used and did not work [link] (https://stackoverflow.com/questions/5756232/moving-lat-lon-text-columns-into-a-point-type-column). Unfortunately, I still get an error with the instruction provided. `ALTER TABLE hospitals_healthcenters_evacuationzones ADD COLUMN point GEOGRAPHY(POINT,26918);` ERROR: Only lon/lat coordinate systems are supported in geography. LINE 2: ADD COLUMN point GEOGRAPHY(POINT,26918); ^ SQL state: 22023 Character: 72 – Alex Dec 01 '19 at 00:16
  • For which reason you specified the SRS `26918` for [NAD83](https://en.wikipedia.org/wiki/North_American_Datum#North_American_Datum_of_1983)? As the error states: This is not supported with type `geography` since not working with lon/lat coordinates. – hc_dev Dec 01 '19 at 00:40
1
 ALTER TABLE hospitals_healthcenters_jointed_to_evacuationzones
     ADD COLUMN shape geometry(Point,4326);

    UPDATE  hospitals_healthcenters_jointed_to_evacuationzones
      SET   shape = st_Setsrid(st_Point("long_"::double precision, "lat"::double precision),4326);
Faleron
  • 21
  • 2