postgis st_geomfromgml, what is wrong with my point? - postgis

In PostGIS 2.0 I try:
select ST_GeomFromGML(
'<gml:Point srsName="urn:ogc:def:crs:EPSG::28992">
<gml:pos>275466.0 565559.0 0.0</gml:pos>
</gml:Point>');
This gives an error:
********** Error **********
ERROR: invalid GML representation
SQL state: XX000
Context: SQL function "st_geomfromgml" statement 1
What is wrong with my point?

It looks like the parser is assuming a 2D geometry, when you actually have a 3D geometry, even though you might not be using the Z dimension, unless your point is at sea level (it is 0.0). If you explicitly state the dimension attribute, you will get the correct result:
SELECT ST_AsText(ST_GeomFromGML(
'<gml:Point srsName="urn:ogc:def:crs:EPSG::28992">
<gml:pos dimension="3">275466.0 565559.0 0.0</gml:pos>
</gml:Point>'));
st_astext
---------------------------
POINT Z (275466 565559 0)
(1 row)

Related

How to calculate distance in "km" using a points geometry column to the nearest linestring geometry column in SQL Server spatial

I am looking to create a field that tells me the nearest neighbour distance in km between 2 geometric columns.
I have a point dataset and multiline dataset both with the geometric fields.
I tried the following:
SELECT
p.GEOM.STDistance(l.GEOM) AS distance
FROM
[points] p, [dbo].[lines] l
after running this query I get the following message
Msg 6522, Level 16, State 1, Line 2
A .NET Framework error occurred during execution of user-defined routine or aggregate "geometry":
System.ArgumentException: 24144: This operation cannot be completed because the instance is not valid. Use MakeValid to convert the instance to a valid instance. Note that MakeValid may cause the points of a geometry instance to shift slightly.
System.ArgumentException:
Could you help me with this one please?
Much appreciated!
Thanks!
The error seems to indicate you have bad spatial data in one of the tables but the query you're using has issues as well. Run this
select ID, geom.STisValid() from lines
and
SELECT ID, geom.STisValid() from points
To see if you have any bad geometry records. If you do run
update table 'with bad spatial datatype'
set geom = geom.MakeValid()
Once you're sure the spatial data is valid rework your query to use a cross join (compare every line to every point (and get the distance) and store the results in a temp table.
Query the temp table for the min(distance) for each point or line. Provided you only need to know the closest feature and not a distance you can do all this with geometry data. If you need to have a measure in feet or meters you will need to convert your data from geometry to geography.
If you have a large number of features in either table the cross join could take forever. Try to work it out on a subset.

postgis - using field name in ST_POINT

I'm trying to run this:
SELECT
*
FROM
creator.maps
WHERE
ST_DISTANCE(
ST_POINT(10,10)::geography,
ST_POINT(geolocation)::geography
)
< 50000;
But it's giving me the error:
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
geolocation is a POINT type
How do I fix this?
Error:
ERROR: function st_point(point) does not exist
The type point can be converted to a PostGIS geometry first, then to a geography.
SELECT
*
FROM
creator.maps
WHERE
AND ST_DISTANCE(
ST_POINT(10,10)::geography,
geolocation::geometry::geography
)
< 50000;
Note that Postgres native geometric functions are fairly limited so you may want to consider storing data as geometry(point) instead of point

Find rows with exact geography coordinates

I have a SQL table with a geography column. When I look at one of the rows the geography shows as a long hex string: 0xE6100....C0.
I want to write a query that finds all other rows in my database that have this same value. How can I do this?
I tried adding WHERE location = '0xE6100....C0' with and without quotes but I get an error:
Invalid operator for data type. Operator equals equal to, type equals geography.
Note: I'm just doing this query in an ad-hoc fashion I'm not really looking for a optimal solution or a way to parameterize this in any way. I just have a row that I'd like to find related values.
Looks like you need to use .STEquals
Check the documentation here

How to fix locale errors in RODBC (WAS: R CMD BATCH: "$ operator is invalid for atomic vectors" but not in Rstudio)

Maybe related: Stack overflow: Windoes does not support UTF-8
I have a script which I can source from Rstudio, but when I try to source it from Rgui.exe or try to BATCH CMD run, I get the following error in my Rout file:
Error in easy_clean$Sv_Karakter: $ operator is invalid for atomic vectors
The reason is that the database table I am quering have a latin charachter 'ø' in its name (se third line below). So the result of my query is this (as per str(easy)):
"";"x"
"1";"42000 102 [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '¸'."
"2";"[RODBC] ERROR: Could not SQLExecDirect 'select *
from PrøveDeltager a
left outer join Aftaler b
ON b.Cpr = a.CPR
LEFT OUTER JOIN GODK c
ON c.GODK_ID = b.GODK_ID
where a.slut >= '20140808'
AND a.slut <='20140818'
AND a.Branche = 'vvs'
AND a.SaleID is not null
AND a.CPR in (select x.CPRNR from Statistik x)
order by Sv_Karakter'"
In rstudio the query works.
Sys.getlocale('LC_CTYPE')returns Danish_Denmark.1252 in both R.gui and Rstudio - so I don't know how to fix this.
I did find this link to developer.r-project which discus windows locales (quite old though).
For now I have created a database view without the 'ø' - that view I can call without problems from R.
From sessionInfo I can say that:
Rstudio R is 64 bit, and R.exe is 32 bit.
Other than that, the only difference is this, for Rstudio:
loaded via a namespace (and not attached): 1 tools_3.1.0
Since I can't write my database credentials, I can't create a reproducible example. But here is the script. http://pastebin.com/XwdZPhL7
Two ways I can imagine that would yield an error with that code: one is if this results in a single column:
easy[!is.na(easy$Sv_Karakter),]
In that case, the result would be a vector because of the default action of the "[" function to create atomic vectors from single column results. The attempts to extract that column would fail with that error. The other case of failure (but perhaps not that error) would be when there was no 'Sv_Karakter' column in the 'easy' dataframe.
Better efforts at documentation by offering str(easy) or dput(head(easy)) are needed.
Your query returned an error message and it was in the form of an R vector. So that explains the particular error. You now need to figure out how your db connection is getting messed up, as Andreas was saying.

Casting a Geography type in PostGIS

I have the following table defined, which uses PostGIS geography.
CREATE TABLE test_geog (
id SERIAL PRIMARY KEY,
boundary GEOGRAPHY(Polygon)
);
I added the following test polygon to the table:
INSERT INTO test_geog VALUES (ST_GeographyFromText('SRID=4326;POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))'));
I am trying to determine whether a point lies within any of the polygons in this table. I have this query:
SELECT ST_ContainsProperly(ST_GeographyFromText('Point(2 2)'), area)
FROM (SELECT boundary FROM test_geog) AS area;
This yields the following error:
ERROR: function st_containsproperly(geography, record) does not exist
LINE 1: SELECT ST_ContainsProperly(ST_GeographyFromText('Point(2 2)'...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
How do I convert this "record" into a POLYGON? I'm confused because it seems like the column has already been declared to only hold POLYGON types, but for some reason that is not what I am pulling out of the database.
I attempted to cast the record to a POLYGON like this:
SELECT ST_ContainsProperly(ST_GeographyFromText('Point(2 2)'), CAST (boundary AS POLYGON))
FROM (SELECT boundary FROM source_imagery) AS nitf_area;
But that gives me this error:
ERROR: cannot cast type record to polygon
LINE 1: ...tainsProperly(ST_GeographyFromText('Point(2 2)'), CAST (boun...
What am I not understanding here?
My question got answered over on the GIS forum. I made the silly mistake of thinking ST_ContainsProperly could be used for Geography, when it is actually only supported for Geometry. I also didn't need the superfluous sub-query I was trying to do.
So here is my solution. I switched over to use ST_Covers instead, which does what I wanted and supports Geography. This avoids casting to Geometry. Here's the code that works:
SELECT ST_Covers(
boundary,
ST_GeographyFromText('Point(2 2)')
)
FROM source_imagery

Resources