Home > other >  Split Polygons by Overlap in Python
Split Polygons by Overlap in Python

Time:11-23

I have the Json data that I want to Split by overlap polygons

enter image description here

data_01 = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [[2, 2], [2, 22], [22, 22], [22, 2], [2, 2]]
                ]
            },
            "properties": {"z": 1412.5, "la": "ba"}
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [[12, 16], [7, 10], [17, 10], [12, 16]]
                ]
            },
            "properties": {"z": 1412.5, "la": "ba"}
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [[27, 15], [24, 12], [29, 12], [27, 15]]
                ]
            },
            "properties": {"z": 1412.5, "la": "ba"}
        }
    ]
}

enter image description here

I would like to get the data where data from Poly_1 and 2 should be joined like data_final:

I try to read data

import json

with open("data_01.json", 'r', encoding='utf-8-sig') as fh:
    d = fh.read()
    f = json.loads(d)

j = f['features'][0:]

for i in j:
    poly_coord = i['geometry']['coordinates'][0:]
    poly_coord = poly_coord[0]
    print(poly_coord )
data_final = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [[27, 15], [24, 12], [29, 12], [27, 15]]
                ]
            },
            "properties": {"z": 1412.5, "la": "ba"}
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [[2, 2], [2, 22], [22, 22], [22, 2], [2, 2]],
                    [[12, 16], [7, 10], [17, 10], [12, 16]]
                ]
            },
            "properties": {"z": 1412.5, "la": "ba"}
        }
    ]
}

CodePudding user response:

You can read/write GeoJSON objects and do spatial set operations like this with geopandas:

In [8]: df = gpd.read_file("data_01.json", engine="GeoJSON")

In [9]: df
Out[9]:
        z  la                                           geometry
0  1412.5  ba  POLYGON ((2.00000 2.00000, 2.00000 22.00000, 2...
1  1412.5  ba  POLYGON ((12.00000 16.00000, 7.00000 10.00000,...
2  1412.5  ba  POLYGON ((27.00000 15.00000, 24.00000 12.00000...

In [10]: df.loc[0, "geometry"] = (df.loc[0, "geometry"] - df.loc[1, "geometry"])

In [11]: df = df.drop(1)

In [12]:
Out[12]:
        z  la                                           geometry
0  1412.5  ba  POLYGON ((2.00000 22.00000, 22.00000 22.00000,...
2  1412.5  ba  POLYGON ((27.00000 15.00000, 24.00000 12.00000...

You can then export back to json with to_json:

In [13]: print(df.to_json())
Out[13]: 
{
    "type": "FeatureCollection",
    "features": [
        {
            "id": "0",
            "type": "Feature",
            "properties": {"la": "ba", "z": 1412.5},
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [[2.0, 22.0], [22.0, 22.0], [22.0, 2.0], [2.0, 2.0], [2.0, 22.0]],
                    [[7.0, 10.0], [17.0, 10.0], [12.0, 16.0], [7.0, 10.0]]
                ]
            }
        },
        {
            "id": "2",
            "type": "Feature",
            "properties": {"la": "ba", "z": 1412.5},
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [[27.0, 15.0], [24.0, 12.0], [29.0, 12.0], [27.0, 15.0]]
                ]
            }
        }
    ]
}
  • Related