Use PostGIS to retrieve the nearby points - postgresql

I've tried to use PostGIS to retrieve the nearby points, however,
I've gotten an error message from the pgAdmin3.
Can you help me to debug the SQL queries (Postgresql+PostGIS) below?
Thank you for your kindness help.
I've used the 3826 geometry.
"ERROR: parse error - invalid geometry
HINT: "...79721.29349234176 2759680.13418412))" <-- parse error at position 44 within geometry" in the
SELECT * FROM pointslight
WHERE ST_DWithin(
ST_Transform(ST_GeomFromText('POINT(279721.29349234176 2759680.13418412))',3826),26986),
ST_Transform(location,26986), 50)
ORDER BY ST_Distance(ST_GeomFromText('POINT(279721.29349234176 2759680.13418412))',3826), location);

In the string defining the point, you have one opening parenthesis an two closing one. Remove one of the latter!

Related

Mapbox: Getting "Coordinate is invalid: 3.099813715520299,101.63553013450047" error for Matrix API

I am working with matrix API https://docs.mapbox.com/api/navigation/matrix/
When I try to add the coordinates given in above link in my system and try i am getting proper result.
https://api.mapbox.com/directions-matrix/v1/mapbox/driving/-122.42,37.78;-122.45,37.91;-122.48,37.73?approaches=curb;curb;curb&access_token=<%access Token%>&sources=0&destinations=1,2
But when I am trying realtime data I am getting below error
https://api.mapbox.com/directions-matrix/v1/mapbox/driving/3.08,101.58;3.09,101.57;3.10,101.63;3.06,101.55;3.13,101.68;3.10,101.66?access_token=<%access token%>&sources=0&destinations=1,2,3,4
{
message: "Coordinate is invalid: 3.08,101.58",
code: "InvalidInput"
}
Can please help me with what the issue with coordinates
Fixed the issue.
I was using Lat,Long but map box is expecting long,lat.
So below URL worked for me
https://api.mapbox.com/directions-matrix/v1/mapbox/driving/101.63553013450047,3.099813715520299;101.65939106660886,3.1136978510187237;101.61475910726944,3.0845580973251456;101.67690052758046,3.1154119291453943;101.67346729993896,3.1075271466768917;101.66076435766543,3.083872446415484?access_token=<%AccessToken%>&sources=0&destinations=1;2;3;4
One thing also causing the Coordinate is invalid error is a redundant ; after the last pair of coordinates, so instead of e.g.
-117.17282,32.71204;-117.1470279,32.7048179; use
-117.17282,32.71204;-117.1470279,32.7048179

PostGIS ST_OffsetCurve fails for some lines

Using PostgreSQL and PostGIS I get an error when using ST_OffsetCurve for some specific line geometries:
"lwgeom_offsetcurve: noded geometry cannot be offset"
Example:
SELECT ST_OffsetCurve(ST_GeomFromText('LINESTRING(288249.918098328 5615135.51135102,288293.425429089 5615086.62800628,288231.885629119 5615155.77174104,288249.918098328 5615135.51135102)'), -5.6875)
This is a closed line and the last three points are on a straight line.
PostGIS version is 3.0 on Windows.
Changing any coordinates or even just the offset value slightly removes this error.
Is it possible to prevent the error without changing numeric values?
Update to PostGIS version 3.1 solved the issue.
maybe ST_MakeValid fix this problem
https://postgis.net/docs/ST_MakeValid.html
And it is also better to pass srid as second parametr in ST_GeomFromText
SELECT ST_OffsetCurve(ST_MakeValid(ST_GeomFromText(...., your srid)), -5.6875)

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.

Merge multiple geometry lines into a single geometry line

Now I have 3 lines (LINESTRING) with 3 different geometry values.
Line#1 : "11AABB"
Line#2 : "22CCDD"
Line#3 : "33EEFF"
How to merge these lines into a single line via PostGIS function ?
I use to know that It use ST_LineMerge or ST_Union But I'm not sure how to use it with geometry values.
could you be more specific?
with the examples you provided you could try this:
SELECT ST_Union('11AABB'::geometry , ST_Union('22CCDD'::geometry,'33EEFF'::geometry ) );
your geometry values are invalid, but this is the correct sintax of the query you want. You should get this error with the example you provided: "ERROR: parse error - invalid geometry"

postgis shape file import problems

Hi I'm trying to import a shape file from
http://www.nyc.gov/html/dcp/html/bytes/bytesarchive.shtml
into a postgis database. the above files creates MULTIPOLYGONS when i import using shp2pgsql.
then i'm trying to simply determine if lat/long points are contained in my multipolygons
however my select's are not working, and when i print out the poitns of my the_geom column it seems to be very broken.
select st_astext(geom) from (select (st_dumppoints(the_geom)).* from nybb where borocode =1) foo;
gives the result...
st_astext
------------------------------------------
POINT(1007193.83859999 257820.786899999)
POINT(1007209.40620001 257829.435100004)
POINT(1007244.8654 257833.326199993)
POINT(1007283.3496 257839.812399998)
POINT(1007299.3502 257851.488900006)
POINT(1007320.1081 257869.218500003)
POINT(1007356.64669999 257891.055800006)
POINT(1007385.6197 257901.432999998)
POINT(1007421.94509999 257894.084000006)
POINT(1007516.85959999 257890.406100005)
POINT(1007582.59110001 257884.7861)
POINT(1007639.02150001 257877.217199996)
POINT(1007701.29170001 257872.893099993)
...
for points in nyc, this is very off.. what am i doing wrong?
The points are not of. The spatial data that is referred to is NOT in lat/long. This is why numbers are different from what you expect. If you need it to be in long/lat it must be reprojected. See more here: http://postgis.refractions.net/news/20020108/
The projection of the data seems to be in the NAD_1983_StatePlane_New_York_Long_Island_FIPS_3104_Feet coordinate system (according to the metadata - see code.).
<spref>
<horizsys>
<planar>
<planci>
<plance Sync="TRUE">coordinate pair</plance>
<coordrep>
<absres Sync="TRUE">0.000000</absres>
<ordres Sync="TRUE">0.000000</ordres>
</coordrep>
<plandu Sync="TRUE">survey feet</plandu>
</planci>
<mapproj><mapprojn Sync="TRUE">Lambert Conformal Conic</mapprojn><lambertc><stdparll Sync="TRUE">40.666667</stdparll><stdparll Sync="TRUE">41.033333</stdparll><longcm Sync="TRUE">-74.000000</longcm><latprjo Sync="TRUE">40.166667</latprjo><feast Sync="TRUE">984250.000000</feast><fnorth Sync="TRUE">0.000000</fnorth></lambertc></mapproj></planar>
<geodetic>
<horizdn Sync="TRUE">North American Datum of 1983</horizdn>
<ellips Sync="TRUE">Geodetic Reference System 80</ellips>
<semiaxis Sync="TRUE">6378137.000000</semiaxis>
<denflat Sync="TRUE">298.257222</denflat>
</geodetic>
<cordsysn>
<geogcsn Sync="TRUE">GCS_North_American_1983</geogcsn>
<projcsn Sync="TRUE">NAD_1983_StatePlane_New_York_Long_Island_FIPS_3104_Feet</projcsn>
</cordsysn>
</horizsys>
</spref>
If you work much with spatial data I suggest that you read more about map projection.
I think this is not issue with PostGIS. I checked input esri Shape file nybb.shp with AvisMap Free Viewer and as you see points are weird itself:
However there is something interesting in nybb.shp.xml metadata file:
<spdom>
<bounding>
<westbc Sync="TRUE">-74.257465</westbc>
<eastbc Sync="TRUE">-73.699450</eastbc>
<northbc Sync="TRUE">40.915808</northbc>
<southbc Sync="TRUE">40.495805</southbc>
</bounding>
<lboundng>
<leftbc Sync="TRUE">913090.770096</leftbc>
<rightbc Sync="TRUE">1067317.219904</rightbc>
<bottombc Sync="TRUE">120053.526313</bottombc>
<topbc Sync="TRUE">272932.050103</topbc>
</lboundng>
</spdom>
I am not familiar with those toolkit (ESRI ArcCatalog), but most probably you need to rescale your points after import using that metadata.