0

I'm a beginner in PostGIS and GeoJSON.

I need to extract every geometry from GeoJSON and store them in the PostGIS database as polygons.

For each entry in GeoJSON I would like to be able to store its geometry in the database separately as it is in JSON.

In the database the geometry type is:

ALTER TABLE public.a_final ADD COLUMN geometry geometry (Polygon, 4326);

What I have done so far:

$geojson = file_get_contents("o/1.geojson");
//echo $geojson;
$array = json_decode($geojson, TRUE);

echo "<pre>";
print_r($array);
echo "</pre>";    


foreach($array['features'] as $type){

    $layer = $type['properties']['Layer'];
    $SubClasses = $type['properties']['SubClasses'];
    $ExtendedEntity = $type['properties']['ExtendedEntity'];
    $Linetype = $type['properties']['Linetype'];
    $EntityHandle = $type['properties']['EntityHandle'];
    $Text = $type['properties']['Text'];
    $geometry = $type['geometry'];




}


Array
(
    [type] => FeatureCollection
    [features] => Array
        (
            [0] => Array
                (
                    [type] => Feature
                    [properties] => Array
                        (
                            [Layer] => Green Area
                            [SubClasses] => AcDbEntity: AcDbPolyline
                            [ExtendedEntity] =>
                            [Linetype] =>
                            [EntityHandle] => 6B
                            [Text] =>
                        )

                    [geometry] => Array
                        (
                            [type] => LineString
                            [coordinates] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => 708305.979
                                            [1] => 385139.794
                                        )

                                    [1] => Array
                                        (
                                            [0] => 708433.3
                                            [1] => 384989.4
                                        )

                                    [2] => Array
                                        (
                                            [0] => 708434.8
                                            [1] => 384987.7
                                        )

                                    [3] => Array
                                        (
                                            [0] => 708400.454
                                            [1] => 384958.526
                                        )

                                    [4] => Array
                                        (
                                            [0] => 708259.989
                                            [1] => 385100.729
                                        )

                                    [5] => Array
                                        (
                                            [0] => 708305.979
                                            [1] => 385139.794
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [type] => Feature
                    [properties] => Array
                        (
                            [Layer] => Green Area
                            [SubClasses] => AcDbEntity: AcDbPolyline
                            [ExtendedEntity] =>
                            [Linetype] =>
                            [EntityHandle] => 6C
                            [Text] =>
                        )

                    [geometry] => Array
                        (
                            [type] => LineString
                            [coordinates] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => 702461.518
                                            [1] => 381980.706
                                        )

                                    [1] => Array
                                        (
                                            [0] => 702436.783
                                            [1] => 381924.255
                                        )

                                    [2] => Array
                                        (
                                            [0] => 702427.496
                                            [1] => 381927.398
                                        )

                                    [3] => Array
                                        (
                                            [0] => 702452.871
                                            [1] => 381985.312
                                        )

                                    [4] => Array
                                        (
                                            [0] => 702461.518
                                            [1] => 381980.706
                                        )

                                )

                        )

                )
  • possible duplicate: check out the first answer in this question: gis.stackexchange.com/questions/172092/… – pdavis Jan 22 at 15:02
  • I have no experience with PHP itself so no answer; if you want to insert that data from within the PHP backend, just construct and execute the INSERT in the above iteration using ST_SetSRID(ST_GeomFromGeoJSON(<$geometry_obj_as_string>), 4326) – ThingumaBob Jan 23 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.