Postgis error on select all intersetcs of a polygon - gis

i can get all polygon on intersects this:-7.9245860488441 -37.122384500713,-7.9245860488441 -37.11904223938,-7.9233955716106 -37.1187601948
SELECT *
FROM propriedades
WHERE ST_Intersects(
ST_GeomFromText(location::geometry),
ST_GeomFromText('POLYGON(-7.9245860488441 -37.122384500713,-7.9245860488441 -37.11904223938,-7.9233955716106 -37.1187601948)'));
propriedades is my table
location is a colunm on are saved geography polygons
the returned error
ERROR: parse error - invalid geometry HINT:
"POLYGON(-7.9245860488441 " <-- parse error at position 25 within
geometry
*** Error ***
ERROR: parse error - invalid geometry SQL state: XX000 Hint:
"POLYGON(-7.9245860488441 " <-- parse error at position 25 within
geometry
solved, i used the geography, no geometry:
SELECT *
FROM propriedades
WHERE ST_Intersects(
location,
'POLYGON((-7.9239281216632 -37.118491021708,-7.9239281216632 -37.116096663161,-7.9240937813677 -37.116105295265,-7.9241591799314 -37.116171875421,-7.9246636046819 -37.116271405188,-7.9259677455811 -37.118572530098,-7.9256079206171 -37.118728486796,-7.9243335012771 -37.118675962065,-7.9243194973485 -37.11849254918,-7.9242705964918 -37.118491021708,-7.9239281216632 -37.118491021708))'
)
but i have other problem, i want intersects > 50 meters
like this http://slimber.com/gallery/pictures2/23/235563/polygons.jpg

There are two problems with the WKT of the polygon, which is why it cannot be parsed. Polygons need two depths of parentheses, and the last coordinate needs to match the first coordinate to make a closed linear ring (i.e., triangles require four points). The WKT should look like this:
POLYGON((-7.9245860488441 -37.122384500713, -7.9245860488441 -37.11904223938,
-7.9233955716106 -37.1187601948, -7.9245860488441 -37.122384500713))
Also ST_GeomFromText(location::geometry) is really unnecessary, since ::geometry is already a PostgreSQL cast operator to geometry. If it is a geography type, then location::geometry should be sufficient to cast it as geometry.

Related

Invalid MySQL axis-order on official doc site

During learning SPATIAL data in MySQL v.8.0 find error in docs on page .
This query:
SET #json = '{ "type": "Point", "coordinates": [102.0, 0.0]}';
SELECT ST_AsText(ST_GeomFromGeoJSON(#json));
should return WKT with this axis-order:
POINT(102 0)
but, it return this order:
POINT(0 102)
Maybe I make some mistake, waiting for your help.
Fiddle https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b5a9de67da72e55cc43c6752920616f4 .
The display problem is related to srid optional parameter of ST_GeomFromGeoJSON() which has value a default value of 4326 (stands for EPSG:4326 Coordinate system "WGS84, unprojected"). It displays in latitude, longitude order.
Mostly, the data is stored in EPSG:4326 and displayed in EPSG:3857 (Pseudo-Mercator, Spherical Mercator or Web Mercator) in which the components ( latitude, longitude ) are ordered reversely.
So, you need to use such a way
SELECT ST_AsText(ST_GeomFromGeoJSON(#JSON,1,3857));
in order to display in longitude, latitude order.
Demo

Removing characters from column in pandas data frame

My goal is to (1) import Twitter JSON, (2) extract data of interest, (3) create pandas data frame for the variables of interest. Here is my code:
import json
import pandas as pd
tweets = []
for line in open('00.json'):
try:
tweet = json.loads(line)
tweets.append(tweet)
except:
continue
# Tweets often have missing data, therefore use -if- when extracting "keys"
tweet = tweets[0]
ids = [tweet['id_str'] for tweet in tweets if 'id_str' in tweet]
text = [tweet['text'] for tweet in tweets if 'text' in tweet]
lang = [tweet['lang'] for tweet in tweets if 'lang' in tweet]
geo = [tweet['geo'] for tweet in tweets if 'geo' in tweet]
place = [tweet['place'] for tweet in tweets if 'place' in tweet]
# Create a data frame (using pd.Index may be "incorrect", but I am a noob)
df=pd.DataFrame({'Ids':pd.Index(ids),
'Text':pd.Index(text),
'Lang':pd.Index(lang),
'Geo':pd.Index(geo),
'Place':pd.Index(place)})
# Create a data frame satisfying conditions:
df2 = df[(df['Lang']==('en')) & (df['Geo'].dropna())]
So far, everything seems to be working fine.
Now, the extracted values for Geo result in the following example:
df2.loc[1921,'Geo']
{'coordinates': [39.11890951, -84.48903638], 'type': 'Point'}
To get rid of everything except the coordinates inside the squared brackets I tried using:
df2.Geo.str.replace("[({':]", "") ### results in NaN
# and also this:
df2['Geo'] = df2['Geo'].map(lambda x: x.lstrip('{'coordinates': [').rstrip('], 'type': 'Point'')) ### results in syntax error
Please advise on the correct way to obtain coordinates values only.
The following line from your question indicates that this is an issue with understanding the underlying data type of the returned object.
df2.loc[1921,'Geo']
{'coordinates': [39.11890951, -84.48903638], 'type': 'Point'}
You are returning a Python dictionary here -- not a string! If you want to return just the values of the coordinates, you should just use the 'coordinates' key to return those values, e.g.
df2.loc[1921,'Geo']['coordinates']
[39.11890951, -84.48903638]
The returned object in this case will be a Python list object containing the two coordinate values. If you want just one of the values, you can slice the list, e.g.
df2.loc[1921,'Geo']['coordinates'][0]
39.11890951
This workflow is much easier to deal with than casting the dictionary to a string, parsing the string, and recapturing the coordinate values as you are trying to do.
So let's say you want to create a new column called "geo_coord0" which contains all of the coordinates in the first position (as shown above). You could use a something like the following:
df2["geo_coord0"] = [x['coordinates'][0] for x in df2['Geo']]
This uses a Python list comprehension to iterate over all entries in the df2['Geo'] column and for each entry it uses the same syntax we used above to return the first coordinate value. It then assigns these values to a new column in df2.
See the Python documentation on data structures for more details on the data structures discussed above.

Escape quotes inside quoted fields when parsing CSV in Flink

In Flink, parsing a CSV file using readCsvFile raises an exception when encountring a field containing quotes like "Fazenda São José ""OB"" Airport":
org.apache.flink.api.common.io.ParseException: Line could not be parsed: '191,"SDOB","small_airport","Fazenda São José ""OB"" Airport",-21.425199508666992,-46.75429916381836,2585,"SA","BR","BR-SP","Tapiratiba","no","SDOB",,"SDOB",,,'
I've found in this mailing list thread and this JIRA issue that quoting inside the field should be realized through the \ character, but I don't have control over the data to modify it. Is there a way to work around this?
I've also tried using ignoreInvalidLines() (which is the less preferable solution) but it gave me the following error:
08:49:05,737 INFO org.apache.flink.api.common.io.LocatableInputSplitAssigner - Assigning remote split to host localhost
08:49:05,765 ERROR org.apache.flink.runtime.operators.BatchTask - Error in task code: CHAIN DataSource (at main(Job.java:53) (org.apache.flink.api.java.io.TupleCsvInputFormat)) -> Map (Map at main(Job.java:54)) -> Combine(SUM(1), at main(Job.java:56) (2/8)
java.lang.ArrayIndexOutOfBoundsException: -1
at org.apache.flink.api.common.io.GenericCsvInputFormat.skipFields(GenericCsvInputFormat.java:443)
at org.apache.flink.api.common.io.GenericCsvInputFormat.parseRecord(GenericCsvInputFormat.java:412)
at org.apache.flink.api.java.io.CsvInputFormat.readRecord(CsvInputFormat.java:111)
at org.apache.flink.api.common.io.DelimitedInputFormat.nextRecord(DelimitedInputFormat.java:454)
at org.apache.flink.api.java.io.CsvInputFormat.nextRecord(CsvInputFormat.java:79)
at org.apache.flink.runtime.operators.DataSourceTask.invoke(DataSourceTask.java:176)
at org.apache.flink.runtime.taskmanager.Task.run(Task.java:559)
at java.lang.Thread.run(Thread.java:745)
Here is my code:
DataSet<Tuple2<String, Integer>> csvInput = env.readCsvFile("resources/airports.csv")
.ignoreFirstLine()
.ignoreInvalidLines()
.parseQuotedStrings('"')
.includeFields("100000001")
.types(String.class, String.class)
.map((Tuple2<String, String> value) -> new Tuple2<>(value.f1, 1))
.groupBy(0)
.sum(1);
If you cannot change the input data, then you should turn off parseQuotedString(). This will simply look for the next field delimiter and return everything in between as a string (including the quotations marks). Then you can remove the leading and trailing quotation mark in a subsequent map operation.

how to access multipolygon coordinates in mysql

Could you please help me to find the right solution how to access to the individual points of multi-polygon object in mysql? Here is the object:
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[-105.00432014465332, 39.74732195489861],
[-105.00715255737305, 39.74620006835170],
[-105.00921249389647, 39.74468219277038],
[-105.01067161560059, 39.74362625960105],
[-105.01195907592773, 39.74290029616054],
[-105.00989913940431, 39.74078835902781],
[-105.00758171081543, 39.74059036160317],
[-105.00346183776855, 39.74059036160317],
[-105.00097274780272, 39.74059036160317],
[-105.00062942504881, 39.74072235994946],
[-105.00020027160645, 39.74191033368865],
[-105.00071525573731, 39.74276830198601],
[-105.00097274780272, 39.74369225589818],
[-105.00097274780272, 39.74461619742136],
[-105.00123023986816, 39.74534214278395],
[-105.00183105468751, 39.74613407445653],
[-105.00432014465332, 39.74732195489861]
],[
[-105.00361204147337, 39.74354376414072],
[-105.00301122665405, 39.74278480127163],
[-105.00221729278564, 39.74316428375108],
[-105.00283956527711, 39.74390674342741],
[-105.00361204147337, 39.74354376414072]
]
],[
[
[-105.00942707061768, 39.73989736613708],
[-105.00942707061768, 39.73910536278566],
[-105.00685214996338, 39.73923736397631],
[-105.00384807586671, 39.73910536278566],
[-105.00174522399902, 39.73903936209552],
[-105.00041484832764, 39.73910536278566],
[-105.00041484832764, 39.73979836621592],
[-105.00535011291504, 39.73986436617916],
[-105.00942707061768, 39.73989736613708]
]
]
]
}
This is the actually array of two objects, where the first object has two geometries and the second one. I can access to the geometries of the first object with these queries SELECT AsText( GeometryN( geo_type, 1)) FROM polygon_park; and SELECT AsText( GeometryN( geo_type, 1)) FROM polygon_park; and I get this result
POLYGON((-105.00432014465332 39.74732195489861),(-105.00715255737305 39.7462000683517),(-105.00921249389647 39.74468219277038),(-105.01067161560059 39.74362625960105),(-105.01195907592773 39.74290029616054),(-105.00989913940431 39.74078835902781),(-105.00758171081543 39.74059036160317),(-105.00346183776855 39.74059036160317),(-105.00097274780272 39.74059036160317),(-105.00062942504881 39.74072235994946),(-105.00020027160645 39.74191033368865),(-105.0007152557373 39.74276830198601),(-105.00097274780272 39.74369225589818),(-105.00097274780272 39.74461619742136),(-105.00123023986816 39.74534214278395),(-105.00183105468751 39.74613407445653),(-105.00432014465332 39.74732195489861))
and
POLYGON((-105.00361204147337 39.74354376414072),(-105.00301122665405 39.74278480127163),(-105.00221729278564 39.74316428375108),(-105.00283956527711 39.74390674342741),(-105.00361204147337 39.74354376414072))
, but I do not know how to access to the second object with the following coordinates.
"[-105.00361204147337, 39.74354376414072],
[-105.00301122665405, 39.74278480127163],
[-105.00221729278564, 39.74316428375108],
[-105.00283956527711, 39.74390674342741],
[-105.00361204147337, 39.74354376414072]"
However in both cases I cannot access to the individual points in each geometry.
I need to know this in order to parse this object into json in php.
Thank you a lot in advance!
To access the second object of the first polygon, you use the InteriorRingN(poly, index) where index is 1 based, see the docs for Polygon functions.
So, in your case, you would do:
SELECT AsText( InteriorRingN(GeometryN(geo_type, 1), 1)) FROM polygon_park;
To get individual points use the PointN function of a Linestring. You first have to convert your Polygon rings to Linestrings, for which you can use the Exteriorring or InteriorRingN functions, and then you can access the points.
So, for example, to get the 4th point, of the outer ring of the first polygon, you would do:
SELECT AsText( PointN(ExteriorRing(GeometryN(geo_type, 1)), 4)) FROM polygon_park;
To get the actual values, rather than textual representation, you would use the X and Y functions instead of AsText.
It would possibly have been clearer to use the WKT rather than GeoJSON for you examples, but seeing as there is a one to one mapping between the meaning of parenthesis in WKT and square brackets in GeoJSON, nothing is lost in translation, so to speak.

receiving nth character when trying to access JSON values

Having trouble with accessing json values formed from php array
var latlag = '<?php echo json_encode($coordinates); ?>';
alert(latlng) produces:
[{
"1280":{"lat":"-1.197070","lng":"-1.197070"},
"1239":{"lat":"-1.222410","lng":"-1.222410"},
"1258":{"lat":"-1.153020","lng":"-1.153020"},
...
}]
I've tried all sorts of ways to access lat and lag for a specific ID and the only result other than undefined has been the nth character of latlng as if its being treated like a string?!
alert(latlng[10]); # {
alert(latlng[1280]['lat]); # undefined
alert(latlng['1280'].lat); # undefined
You don't want to put the JSON in quotes, so:
var latlag = <?php echo json_encode($coordinates); ?>;
(Technically, that's not JSON at all, it's a JavaScript object initializer. But that's fine, JSON is a subset of initializer syntax and so all valid JSON texts are also valid JavaScript initializers.)
If the structure is really as you've quoted it, it's an array with one entry, which is an object with properties with names like 1280 and 1258, whose values are objects with properties named lat and lng. So you'd access those like this:
alert(latlng[0]["1280"].lat);
latlng is the array, latlng[0] is the one object it holds, and latlng[0]["1280"] is the {"lat":"-1.197070","lng":"-1.197070"} object.
You may be wondering why I've used quotes around 1280 above. It's because those keys are clearly given as strings (as is required in JSON, though not in JavaScript initializers), and so I can't be sure there aren't entries like "0012". Property names are always strings even when not written as strings, so latlng[0][1280] and latlng["0"]["1280"] both mean the same thing (because the 0 and the 1280 are converted to string [yes, really]), but naturally latlng[0]["0012"] is not the same as latlng[0][12] because the latter uses "12", not "0012", as the property name. If you know you won't have leading zeros, you can ditch the quotes.