st_intersect() not working in postgresql - postgresql

i am using postgresql version :
"PostgreSQL 9.3.1 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2), 64-bit"
i have created 2 tables A and B with point and polygon as a data type.
now i want to know whether point is inside the polygon or not.
for this i am trying to use ST_Intersect(A.point_LatLong , B.polygon_abc);
my query is :
SELECT A.id
FROM A, B
WHERE A.name = 'callifornia'
AND ST_Intersect(A.point_LatLong , B.polygon_abc);
here point_latLong and polygon_abc are column name having datatype point and polygon in table A and B.
but this query gives an error :
ERROR: function st_intersect(point, polygon) does not exist
LINE 3: WHERE city.city_name = 'callifornia' AND ST_intersect(city.c...
HINT: No function matches the given name and argument types. You might need to add
explicit type casts.
How can I solve this problem? I even not be able to use any other spatial method in postgresql like st_contains() etc let me know if you have any solution.

You are trying to mix PostgreSQL's built-in (limited, but useful) geometric types with PostGIS functions. It sounds like you didn't install PostGIS, either. You've also typo'd the function name, it's ST_Intersects not ST_Intersect.
First, if you want to use PostGIS, make sure it's installed and then:
CREATE EXTENSION postgis;
Next, you'll probably find that you can't actually call ST_Intersects with a point and a polygon. PostGIS works with its own geometry type. It has some converters for the PostgreSQL internal types, but they're only limited. So calling PostGIS functions with primitive geometric types can result in errors like:
postgres=# SELECT ST_Intersects( polygon( box(point(0,0), point(10,10)) ), point(5,5) );
ERROR: function st_intersects(polygon, point) does not exist
LINE 1: SELECT ST_Intersects( polygon( box(point(0,0), point(10,10))...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
You'll often have to convert them to PostGIS's own geometry type. PostGIS provides some explicit casts for this for most types, e.g.:
postgres=# SELECT ST_Intersects(
polygon( box(point(0,0), point(10,10)) )::geometry,
point(5,5)::geometry
);
st_intersects
---------------
t
(1 row)
So in your query, that'd be:
ST_Intersects(A.point_LatLong::geometry , B.polygon_abc::geometry);

Related

Function createtopology(unknown, integer, integer) does not exist. Not able run PostGIS function in Postgres

I am trying to run this function
SELECT public.CreateTopology('topo1',4326,0);
which gives me
ERROR: function public.createtopology(unknown, integer, integer) does not exist
LINE 1: select public.CreateTopology('topo1',4326,0);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 8
I can use other PostGIS function without trouble. This one however does not work. Note, there are many schems in my database. Why is that?
The third parameter is optional, but if present is data type double precision, if omitted defaults to 0. (see Postgis documentation CreateTopology. So try
SELECT public.CreateTopology('topo1',4326,0::double precision);
OR
SELECT public.CreateTopology('topo1',4326,0.0);
OR
SELECT public.CreateTopology('topo1',4326);
UPDATE: As posted 'topo1' is a string literal. But it needs to be Topology. You need to correct the data type.

PostgreSQL Function Error: No function matches the given name and argument types. You might need to add explicit type casts

I am currently new to PostgreSQL and I am implementing pgrouting using Esri Shapefiles.
The query I am writing is :
SELECT pgr_createTopology('point_file'::text,0.000001,'the_geom'::text, 'gid'::text);
"point_file" is the table name ,"the_geom" is the geometry column, and "gid" is the id column.
The error I am getting is :
ERROR: function pgr_createtopology(text, numeric,text, text) does not exist
LINE 1: SELECT pgr_createTopology('point_file'::text,0.000001,'th...
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 8
NOTE: I have already added postgis as an extension.
I am using PostgreSQL 13 AND PostGIS 3.1.

how to add a point using raw query?

I need to insert a point into table of type postgres, I tried:
\DB::statement("SET search_path = postgis, public;");
\DB::statement("INSERT INTO points (latlong) VALUES( ST_GeomFromText('POINT(-71.060316 48.432044)', 4326));");
but I got error:
SQLSTATE[42883]: Undefined function: 7 ERROR: function st_geomfromtext(unknown, integer) does not exist
LINE 1: INSERT INTO points (latlong) VALUES(ST_GeomFromText('POINT(-...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts. (SQL: INSERT INTO points (latlong) VALUES(ST_GeomFromText('POINT(-71.060316 48.432044)', 4326));)
I am using postgres 9.4.5 with postgis installed .. any idea?
Install PostGIS using something like CREATE EXTENSION IF NOT EXISTS postgis
Creating a geometry by formatting a WKT string for ST_GeomFromText is not the best way to make a geometry (slower, lossy, error prone, etc.), unless your source data is already text. To make a point geometry from two floating point values, use something like:
DB::insert('INSERT INTO points (latlong) VALUES(ST_SetSRID(ST_MakePoint(?, ?), 4326))',
[lng, lat]);

saving point type data to pg server

I have a function in postgres that inserts data to a table, and one of the columns is of type point.
I don't know how to save my gotten data to an acceptable form for the function to save my data.
The problematic code in question is :
CREATE OR REPLACE FUNCTION db."setMessage"(eml text, msg text, lat text, lng text)
...
INSERT INTO
db.messages
VALUES
(DEFAULT,
id,
$2,
DEFAULT,
ST_SetSRID(ST_MakePoint($3::float, $4::float), 4326)
);
The error received when i call the function is:
ERROR: function st_makepoint(double precision, double precision) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Context: PL/pgSQL function "setMessage" line 9 at SQL statement
I have tries to save the point in string format ('POINT(12.22343 64.22233)'), tried not casting the st_makepoint arguments, and inserting without the outer function (ST_SetSRID).
could anyone tell me what I'm doing wrong?
SOLUTION:
So the method of success was following this link to get the needed libraries. after that i created the required extensions, and now i have access to the desired functions.
To install PostGIS, follow the instructions for your operating system at https://postgis.net/install.
Then, to enable it on your db you can do CREATE EXTENSION postgis; from psql or PGAdmin.
I had a similar problem, that postgres didn't find the function st_makepoint even though postgis extension was installed and I used the correct types for arguments. It turned out that the search path did not contain postgis.
Solution was: alter database my_db set search_path = my_schema, public, postgis;
Hope it helps somebody.
I tried to re-install postgis extension and have tried many soluations but did not work for me.
I just edit my query and put extensions keyword before functions name like this:
INSERT INTO
db.messages
VALUES
(DEFAULT,
id,
$2,
DEFAULT,
extensions.ST_SetSRID(extensions.ST_MakePoint($3::float, $4::float), 4326)
);
That worked for me.

generate json with PostgreSQL

I have postgreSQL 8.4+PostGIS 1.5.
I want to generate GeoJson. I do:
SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
FROM (SELECT 'Feature' As type
, ST_AsGeoJSON(lg.the_geom)::json As geometry
, row_to_json(lp) As properties
FROM parcels_temp As lg
INNER JOIN (SELECT num, cadastr FROM parcels_temp) As lp
ON lg.num = lp.num ) As f ) As fc;
But get an error:
ERROR: type "json" does not exist
LINE 4: , ST_AsGeoJSON(lg.the_geom)::json As geometry
What am I doing wrong?
There is no json data type in PostgreSQL 8.4. The type was introduced in 9.2, though a backport to 9.1 was created; see this bitbucket.
Use text. json is just a validating wrapper around the text type anyway, the interesting bit is the functions like row_to_json - which are also unavailable for 8.4.
If you can't use text - say, because you're using 3rd party code that expects json, or because you need the json functions - then it's time to upgrade PostgreSQL. 8.4 is getting pretty elderly anyway, as is PostGIS 1.5.