簡體   English   中英

如何在postgis中從php和insrt poligon轉換geojson中的幾何

[英]How to convert geometry in the geojson from php and insrt poligon in postgis

我是Postgis和geojson的初學者,

我應該從geojson中提取每個幾何並將它們存儲為多邊形的postgis數據庫

對於geojson中的每個條目,我希望能夠像在json中一樣將其幾何分別存儲在數據庫中

在數據庫中,幾何類型為:

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

到目前為止,我所做的是:

$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'];




}

GeoJSON的:

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
                                        )

                                )

                        )

                )

您無需完成將GeoJSON轉換為SQL的所有ST_GeomFromGeoJSON() ,只需使用ST_GeomFromGeoJSON()將其扔到PostgreSQL中ST_GeomFromGeoJSON() 然后,您只需執行以下操作:

<?php
//load GeoJSON
$geojson = file_get_contents("o/1.geojson");
//Translate that into JSON-compliant array of features
$features = json_decode($geojson, TRUE)->features;
//Connect to your database
$connection = pg_connect(....);
//Iterate over features within FeatureCollection
foreach($features as $feature) {
    //Extract necessary attributes
    $layer = $feature->properties["Layer"];
    $subClasses = $feature->properties["SubClasses"];
    ....
    //Extract geometry attribute as a string
    $geomJson = json_encode($feature->geometry);
    //Make up SQL-query
    $sql = "insert into mytable (layer, subclasses, ...., geom) values ('".$layer."','".$subClasses."',..., st_geomfromgeojson('".$geomJson."')";
    //Execute the query
    pg_query($connection, $sql);
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM