Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON containing geometry to postgreSQL

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
        ]
      }
    }
      ]
};
like image 267
morteza Avatar asked Oct 19 '25 17:10

morteza


1 Answers

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;  
like image 124
Jim Jones Avatar answered Oct 22 '25 08:10

Jim Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!