How to parse multiple geojson feature types and write to SQL Geography column

拟墨画扇 提交于 2020-01-05 08:27:17

问题


This is the follow up question from here.

Given: A geojson file with 3 different geometry types (Point, Polygon, Line) and multiple properties (AssetType, GeoID, etc.), write each geometry type and its corresponding properties to a single Azure SQL record which uses a Geography-type column.

Work so far: I have separate SQL statements that will push the properties alone, then push the Polygon alone into a Geography-type column. But this results in the properties in one record and the geography in another (the graphic doesn't exactly match the script below, illustrative purposes only).

Questions: How should the scripts be combined to write both properties and geometry to the same record? How should the script account for Point, Polygon and Line geometry types as they are encountered in the geojson file?

Any help would be greatly appreciated.

-- Create the table

IF OBJECT_ID('dbo.fixedAssets', 'U') IS NOT NULL
DROP TABLE dbo.fixedAssets
GO

CREATE TABLE dbo.fixedAssets
(
    Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    GeoId [NVARCHAR] (50),
    AssetType [NVARCHAR](255),
    Status [NVARCHAR](255),
    Desc [NVARCHAR](255),
    GeoType [NVARCHAR](255),
    GeoCoord [GEOGRAPHY]
)
GO

-- Create sample geojson

DECLARE @JsonSample nvarchar(max) = 
'{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
                "geoID": "001",
                "geoType": "Residential",
                "geoStatus": "Active",
                "geoDesc" : "Cool place"
            },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -122.38563537597656,
          47.57368844862113
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
                "geoID": "004",
                "geoType": "Commercial Prod",
                "geoStatus": "Active",
                "geoDesc" : "Don't go here"
            },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -122.33181953430176,
              47.57808903904011
            ],
            [
              -122.32658386230469,
              47.57808903904011
            ],
            [
              -122.32658386230469,
              47.58214188724162
            ],
            [
              -122.33181953430176,
              47.58214188724162
            ],
            [
              -122.33181953430176,
              47.57808903904011
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
                "geoID": "007",
                "geoType": "Road",
                "geoStatus": "Active",
                "geoDesc" : "To nowhere"
            },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -122.39275932312012,
            47.583994513354966
          ],
          [
            -122.40022659301758,
            47.57889963377935
          ],
          [
            -122.39275932312012,
            47.57768373696443
          ],
          [
            -122.40031242370604,
            47.57473072714337
          ]
        ]
      }
    }
  ]
}';

--This works for inserting properties into their own records

INSERT INTO dbo.fixedAssets_DEV (GeoId, AssetType, Status, Desc, GeoType)
SELECT
  GeoId,
    AssetType,
    Status,
    Desc, 
    GeoType
FROM
OPENJSON(@JsonSample, '$.features')
    WITH (
        GeoId [NVARCHAR] (50) '$.properties.geoId',
        AssetType [NVARCHAR](300) '$.properties.Type',
        Status [NVARCHAR](300) '$.properties.Status',
        Desc [NVARCHAR](300) '$.properties.Desc',
        GeoType [NVARCHAR](300) '$.geometry.type'
    )

GO

-- This works for inserting Polygon into Geography column, but its writing all features to one record instead of each record.

INSERT INTO dbo.fixedAssets (GeoCoord)
SELECT
  geography::STPolyFromText('POLYGON ((' + STRING_AGG(CAST(Long + ' ' + Lat as varchar(max)), ',') + '))',4326).ReorientObject() AS fGeofenceCoord
FROM
    (
    SELECT 
        Long,
        Lat
    FROM
        OPENJSON(@JsonSample, '$.features[0].geometry.coordinates[0]')
        WITH
            (
                Long varchar(100) '$[0]',
                Lat varchar(100) '$[1]'
            )
)d

-- Need SQL for inserting Point and Line features into the GEOGRAPHY column. 
-- Need to combine these statements into a single script that can parse a geojson file and store data accordingly

来源:https://stackoverflow.com/questions/59011684/how-to-parse-multiple-geojson-feature-types-and-write-to-sql-geography-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!