Convert JSON containing geometry to postgreSQL - json

I have a JSON file that contains the attribute of several parks along with location (geometry) as a point. I was wondering how to convert the JSON to postgreSQL format. Indeed, I have tried several ways such as SQLizer and MapForce, but I was not able to convert them. Is there any way to convert this JSON which has geometry, to postgreSQL format?
I appreciate any help.
Below you can find the script.
var lenneparks = {
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"place": "Aachen Kurpark",
"year": "1853 (Hi)",
"text": "Elisengarten, kleine Parkanlage in der Innenstadt, rückwärtig vom Elisenbrunnen"
},
"geometry": {
"type": "Point",
"coordinates": [
6.086027,
50.774247
]
}
},
{
"type": "Feature",
"properties": {
"place": "Aachen",
"year": "ca. 1862 (Hi)",
"text": "Staatsprokurator Dubusc"
},
"geometry": {
"type": "Point",
"coordinates": [
6.0838868,
50.7653455
]
}
}
]
};

EDIT 1: Corrected SQL comments
Have you tried the PostGIS extension? It comes with really handy functions to import such data, such as:
-- To create a geometry object from your GeoJSON
SELECT ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}') As geometry;
-- To see the WKT of your GeoJSON
SELECT ST_AsText(ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}')) As geometry;
EDIT 2: Creating records for each geometry
This function will create a table containing one record for each json element in the array features, from there you can start parsing the data you need for creating your tables... I hope it helps:
CREATE TEMPORARY TABLE features AS
SELECT json_array_elements('{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"place": "Aachen Kurpark",
"year": "1853 (Hi)",
"text": "Elisengarten, kleine Parkanlage in der Innenstadt, rückwärtig vom Elisenbrunnen"
},
"geometry": {
"type": "Point",
"coordinates": [
6.086027,
50.774247
]
}
},
{
"type": "Feature",
"properties": {
"place": "Aachen",
"year": "ca. 1862 (Hi)",
"text": "Staatsprokurator Dubusc"
},
"geometry": {
"type": "Point",
"coordinates": [
6.0838868,
50.7653455
]
}
}
]
}'::JSON -> 'features') as features;
SELECT * FROM features;
EDIT 3: Query to extract info from json table
SELECT
features -> 'geometry' -> 'coordinates' -> 0 AS lat,
features -> 'geometry' -> 'coordinates' -> 1 AS lon,
features -> 'properties' -> 'place'::TEXT,
features -> 'properties' -> 'year'::TEXT,
features -> 'properties' -> 'text'::TEXT
FROM features;
EDIT 4: Extracting geometries from the json table and converting them into WKT and Geometry
SELECT ST_GeomFromGeoJSON((features -> 'geometry')::text)
FROM features;
SELECT ST_AsText(ST_GeomFromGeoJSON((features -> 'geometry')::TEXT))
FROM features;

Related

Filtering on GeoJSON data

I was wondering how filtering is normally done on a FeatureCollection for GeoJSON data. For example, take the following earthquake data:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": "ak16994521", "mag": 2.3, "time": 1507425650893, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -151.5129, 63.1016, 0.0 ] } },
{ "type": "Feature", "properties": { "id": "ak16994519", "mag": 1.7, "time": 1507425289659, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -150.4048, 63.1224, 105.5 ] } },
{ "type": "Feature", "properties": { "id": "ak16994517", "mag": 1.6, "time": 1507424832518, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -151.3597, 63.0781, 0.0 ] } },
{ "type": "Feature", "properties": { "id": "ci38021336", "mag": 1.42, "time": 1507423898710, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -118.497, 34.299667, 7.64 ] } },
{ "type": "Feature", "properties": { "id": "hv61900626", "mag": 2.91, "time": 1504833891990, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -155.011833, 19.399333, 2.609 ] } }
]
}
Now, if this data is all within a single FeatureCollection, how would anyone filter the data, for example to view earthquakes with magnitude > 2.5 ? It seems like when dealing with a FeatureCollection the first thing to do would be to extract each of the features into its own item: is that what is usually done, so that individual properties may be queried?
GeoJSON is a transfer format and as you have noticed any operation on it requires you to read and parse the whole file every time. If you plan to do anything with the data you should translate it into a more useful format which supports indexes. If you need to keep to a file based format then I recommend GeoPackage which is supported by the majority of modern GIS. Alternatively, you could use a spatially enabled database such as PostGIS.
In either case the easiest way to convert the data is to use ogr2ogr

Flutter: Unexpected character (at character 1)

In Flutter, no matter what json file i try to decode I get the same above titled error.
Originally I thought it was something with my project but after starting a new template Flutter project I still receive the same error. I have the json file in my root folder and have added them to the pubspec.yaml file:
assets:
- Sample-JSON-data.json
- Sample-employee-JSON-data.json
Main.dart code at the moment:
var jsonString = 'Sample-JSON-data.json';
var response = jsonDecode(jsonString);
print(response);
I have verified my json data on multiple test sites as well as tried various methods in the Flutter Documentation. Json data:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.0763783444317,
-33.98045132346684
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.0763783444317,
-33.98045132346684
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.0763783444317,
-33.98045132346684
]
}
}
]
}
jsonDecode expects an input to be valid JSON serialized into String fe.
{ "message": "Hello World!" }. So this would work: jsonDecode('{"message": "Hello World!"}').
What you are passing to it is a file name, it won't automatically read the file for you. You can check how to do that here:
Flutter - Read text file from assets
Actually you are not loading the data from the json file.
Try:
var jsonString = await rootBundle.loadString('Sample-JSON-data.json')

Couchbase query array

I have the following json saved in my couchbase bucket "geo".
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"ID": "1753242",
"TYPE": "8003"
}
},
{
"type": "Feature",
"properties": {
"ID": "4823034",
"TYPE": "7005"
}
},
{
"type": "Feature",
"properties": {
"ID": "4823034",
"TYPE": "8003"
}
}
]
}
To get all "features" with "properties.TYPE : 8003" I tried the following.
SELECT features FROM geo
WHERE ANY f IN features SATISFIES f.properties.TYPE = "8003" END;
But this returns the whole json document and not just the "features" with the properties.TYPE "8003".
Does anybody know, how to query to get just the matching features with "properties.TYPE": "8003" as result?
The ANY expression in the WHERE-clause is used to only filter the documents of interest. If you want specific project list, you need to write corresponding expression in the projection list as well. The projection in your query is asking for 'features' and hence the whole 'features' array is being returned. You can write following expression on the projection list to get the output you want:
SELECT ARRAY f FOR f IN features WHEN f.properties.TYPE = "8003" END
FROM geo
WHERE ANY f IN features SATISFIES f.properties.TYPE = "8003" END;
[
{
"$1": [
{
"properties": {
"ID": "1753242",
"TYPE": "8003"
},
"type": "Feature"
},
{
"properties": {
"ID": "4823034",
"TYPE": "8003"
},
"type": "Feature"
}
]
}
]
hth,
-Prasad

GeoJSON object naming convention

A GeoJSON object might look something like this:
{
"type": "Point",
"coordinates": [
28.169778,
-25.886587
]
}
We may embed this into some other resource object, say a 'point of interest':
{
"id": "1266f0WqVkGB2BnnVhOvEw",
"name": "Eco Fushion, Witch-Hazel Ave",
"type": "TRANSIT",
"code": "ert222",
"point": {
"type": "Point",
"coordinates": [
28.169778,
-25.886587
]
}
}
Is there any convention as to what this GeoJSON object should be named as when embedded as a field?
In the above example, I have called it point.

Can't fetching records from nested JSON objects in Sencha Touch Application

I'm stuck with issue for getting web-service response from JSON in Sencha Touch.
I'm getting response from server.
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "business_poi_en.1",
"geometry": {
"type": "Point",
"coordinates": [
28.21962354993591,
36.452844361147314
]
},
"geometry_name": "geom",
"properties": {
"status": "M",
"score": 100,
"side": "R",
"ref_id": 0,
"id": null,
"business_p": "AQUARIOUM",
}
},
{
"type": "Feature",
"id": "business_poi_en.2",
"geometry": {
"type": "Point",
"coordinates": [
28.225417523605692,
36.436470953176716
]
},
"geometry_name": "geom",
"properties": {
"status": "M",
"score": 68.44,
"match_type": "A",
"side": "L",
"ref_id": 0,
"id": null,
"business_p": "ZIGOS",
}
},
.... So On ....
I want to fetch data for coordinates from geometry tag, so I can display it on map via these coordinates.I also want to fetch data for business_p from properties tag as displaying title on map.
But, I can't get both of values for coordinates & business_p at same time from same response.
Any idea for getting values at same time ?
Any suggestion will be appreciated.
Please help with this issue.
Thanks in advance.
Your Store's proxy's reader should have correct rootProperty which is common parent of both coordinates and business_p. For that your response should be wrapped into a top level tag like this
{
"featureResponse": [
{
"type": "FeatureCollection",
"features": []
},
{
"type": "Feature",
"features": []
},
]
}
Then you can define reader like this:
reader: {
type: 'json',
rootProperty: 'featureResponse'
}
once you get record from this store, you can go to data or raw children object to fetch required data.