how to pass geometric point as parameter in postgres function? - postgresql

I have some function where I need to pass a point datatype .
somefunc("United States",Point(85.327892 27.703744))
But I am getting error with this.
ERROR: syntax error at or near "27.703744"
SQL state: 42601
Character: 1192

Maybe try add a comma between the function params?
somefunc("United States",Point(85.327892, 27.703744))

I sense some confusion between PostGIS and PostgreSQL's geometric data types. If you are using PostGIS, you need the ST_MakePoint function, which is perfect for parameters:
SELECT ST_SetSRID(ST_MakePoint($lon, $lat), 4326)) AS geom;

Related

Postgresql json functions missing

it seems like my postgres doesn't have the json functions.
to_json('Fred said "Hi."'::text)
generates:
ERROR: syntax error at or near "to_json"
LINE 1: to_json('Fred said "Hi."'::text)
^
SQL state: 42601
Character: 1
Any idea on how to enable or get the functions to work? Thought they where supposed to be there out of the box.
Turns out I had misunderstood the syntas - it should include SELECT, so SELECT to_json('Fred said "Hi."'::text)works.

Postgis: function st_contains does not exist

I am struggling to get a simple Postgresql/Postgis statement to work, I need all points within a polygon (in this case a rectangle)
SELECT * FROM points_table WHERE ST_Contains( ST_GEOMFROMTEXT('POLYGON((51.8121, 0.13712199999997665, 51.9078, 0.21444399999995767))'), points_table.geom)
The error reads
ERROR: function st_contains(geometry, geography) does not exist
LINE 1: SELECT * FROM points_table WHERE ST_Contains( ST_GEOMFRO...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function st_contains(geometry, geography) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 38
The answer from this question amongst others suggests my statement is correct.
Seems you are comparing GEOMETRY and GEOGRAPHY
As in your message error st_contains(geometry, geography)
be sure that your column points_table.geom is a valid GEOMETRY data type and not a GEOGRAPHY data type .. for this chek also for the SR you are using and eventually convert you geomtext as a valid SR for geography
eg assuming you using as SR 4326
SELECT *
FROM points_table
WHERE ST_Contains(
ST_Transform(
ST_GEOMFROMTEXT('POLYGON((51.8121, 0.13712199999997665, 51.9078, 0.21444399999995767))')
,4326)
, points_table.geom)
Although I accepted an answer that solved my statement, I feel it important to share an alternative to the explicit scenario (points within a boundary) as this is something I have struggled to find a solution to elsewhere.
SELECT * FROM points WHERE points.geom && ST_MakeEnvelope(0.13712199999997665, 51.8121, 0.26340800000002673, 51.9135, 4326)
This one is far simpler and yields the results exactly as required, same as the accepted answer.
I solved this problem by joining on public.ST_CONTAINS(geom, coords) instead of ST_CONTAINS(geom, coords)
I don't really understand enough about Postgresql to know why this worked though. I just skimmed https://gis.stackexchange.com/questions/132103/postgis-st-within-does-not-exist and hoped for the best.

How to execute H2db generated statment in postgres

Below statmen generated from h2
INSERT INTO EPSG_ALIAS
(ALIAS_CODE, OBJECT_TABLE_NAME, OBJECT_CODE, NAMING_SYSTEM_CODE, ALIAS, REMARKS)
VALUES
(1431, 'Datum', 6123, 7300, STRINGDECODE('Kartastokoordinaattij\ufffdrjestelm\ufffd (1966)'), NULL);
When i execute the above statment in postgresql i am getting following error.
ERROR: function stringdecode(unknown) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 140
STRINGDECODE is the way of H2 to read 'ASCII written UTF-8 characters'.
For Postgres, it's a simple 'E' before your string:
STRINGDECODE('Kartastokoordinaattij\ufffdrjestelm\ufffd (1966)')
Becomes
E'Kartastokoordinaattij\ufffdrjestelm\ufffd (1966)'

postgresql - center - "No function matches the given name and argument types. You might need to add explicit type casts."

On Slackware 14.0 x86_64 with postgresql-9.2.4 and postgis-2.0.3.
Have loaded essex-latest.osm.pbf into a database.
I want to get the centre of the roads in Essex, but am having problems.
These work OK:
SELECT st_Extent(way), st_area(st_Extent(way)) from planet_osm_roads;
st_extent | st_area
------------------------------------------------+------------------
BOX(-10979.62 6693910.54,144349.79 6822695.15) | 20004037488.3802
SELECT center(box '((-10979.62, 6693910.54),(144349.79, 6822695.15))');
center
-------------------------
(66685.085,6758302.845)
(But I have wondered why the function is center and not st_center).
Anyway, so why doesn't this work:
SELECT center(st_Extent(way)) from planet_osm_roads;
ERROR: function center(box2d) does not exist
LINE 1: SELECT center(st_Extent(way)) from planet_osm_roads;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Cheers,
Peter
Function center gets argument of type box, not box2d that st_Extent returns. With ST_Centroid you doesn't need to call st_Extent even, try ST_Centroid(way)
center is not a PostGIS function, it is a core geometry function for PostgreSQL.
The best way to find the midpoint of a linestring is to use a linear referencing function (e.g., ST_Line_Interpolate_Point() or ST_LineInterpolatePoint() for PostGIS >=2.1).
See also ST_PointOnSurface.

What is wrong with this PostgreSQL statement?

I have the following statement that I need to run on a table which has a geometry column. I am getting a WKT from Oracle using my C# program and then attempting to insert it into PostgreSQL using an npgsql connection.
highways=# INSERT INTO cluster_125m (CELL_GEOM)
VALUES(ST_GeomFromWKT('POLYGON ((80000.0 17280.0, 80125.0 17280.0, 80125.0 17405.0, 80000.0 17405.0, 80000.0 17280.0))'));
I get the following error:
ERROR: function st_geomfromwkt(unknown) does not exist
LINE 1: INSERT INTO cluster_125m (CELL_GEOM) VALUES(ST_GeomFromWKT('...
^
HINT: No function matches the given name and argument types. You might need to
add explicit type casts.
What is the issue here and what can be done about it?
Use function ST_GeomFromText instead of ST_GeomFromWKT.