Home > Blockchain >  Avoid overlapping geospatial polygons
Avoid overlapping geospatial polygons

Time:10-06

Let's say I have a df structured like this


coordinates = {"type":
               ["a","b"],
                "bound":
["POLYGON ((10.539363999999999 43.365892, 10.538892 43.365323, 10.536833 43.363928, 10.536696 43.363371, 10.537307 43.361193, 10.537106 43.360565, 10.534886 43.359623, 10.534587 43.359051, 10.534427 43.358053, 10.533774 43.357612, 10.533429 43.35713, 10.533718 43.356379, 10.534593 43.355854, 10.533964 43.355367, 10.533782 43.354983, 10.534308 43.353337, 10.534263 43.353022, 10.533930999999999 43.352594, 10.533951 43.351423, 10.534102 43.351052, 10.53519 43.350164, 10.535043 43.349716, 10.535639 43.349375, 10.535887 43.348912, 10.535312 43.345687, 10.535032 43.345331, 10.535831 43.344753, 10.535248 43.344131, 10.534851 43.343065, 10.534766 43.34212, 10.533891 43.339719, 10.534133 43.339076, 10.533415 43.338438, 10.533167 43.337918, 10.53318 43.336451, 10.532833 43.335915, 10.53247 43.334263, 10.531761 43.334268, 10.531166 43.332417, 10.530466 43.332039, 10.529987 43.330641, 10.528661 43.330397, 10.528359 43.330185, 10.527991 43.329271, 10.527292 43.328957, 10.526032 43.327416, 10.52335 43.325174, 10.522925 43.325187, 10.522925 43.324842, 10.5215 43.32495, 10.5197 43.32568, 10.51838 43.32592, 10.5157 43.32514, 10.51462 43.32448, 10.51447 43.32393, 10.51453 43.32275, 10.51468 43.32236, 10.5159 43.32081, 10.51646 43.31879, 10.51639 43.31642, 10.51602 43.31557, 10.5156 43.31526, 10.51344 43.31439, 10.51027 43.31409, 10.50796 43.31408, 10.50647 43.31382, 10.50434 43.31315, 10.50229 43.31313, 10.50168 43.31295, 10.50102 43.31255, 10.50066 43.31199, 10.49981 43.30977, 10.49842 43.30727, 10.49663 43.30496, 10.4954 43.30408, 10.49158 43.30329, 10.48831 43.30232, 10.48791 43.302, 10.488399 43.301448, 10.486615 43.302975, 10.484884 43.302534, 10.485042 43.302878, 10.482994 43.304782, 10.482663 43.304724, 10.482886 43.304991, 10.4828 43.305158, 10.481595 43.306412, 10.480434 43.307364, 10.479935 43.307195, 10.480231 43.307488, 10.480127 43.307696, 10.479126 43.3087, 10.47881 43.308672, 10.47885 43.30887, 10.478048 43.309461, 10.477673 43.309304, 10.477878 43.309499, 10.477786 43.309618, 10.476943 43.310227, 10.476225 43.309922, 10.476747 43.310243, 10.476563 43.310584, 10.474496 43.311771, 10.474 43.311494, 10.474228 43.311811, 10.473657 43.312368, 10.473341 43.312548, 10.472824 43.312401, 10.472965 43.312529, 10.472502 43.312977, 10.471031 43.313765, 10.47072 43.313612, 10.470842 43.313809, 10.470249 43.314235, 10.469343 43.314679, 10.469038 43.314637, 10.468523 43.315086, 10.468071 43.315163, 10.467866 43.31504, 10.467517 43.315463, 10.465122 43.316687, 10.463765 43.317708, 10.462973999999999 43.318096, 10.462621 43.318853, 10.462841 43.318868, 10.46278 43.319211, 10.463365 43.31978, 10.466969 43.320409, 10.468894 43.321492, 10.471155 43.32321, 10.472744 43.324135, 10.476031 43.325434, 10.479861 43.326186, 10.48725 43.333303, 10.488329 43.334008, 10.49325 43.339032, 10.495304 43.340752, 10.495874 43.341339, 10.496127 43.342119, 10.496795 43.342705, 10.497031 43.34325, 10.49787 43.343186, 10.498219 43.343415, 10.498161 43.344037, 10.498266 43.344306, 10.498728 43.344525, 10.498637 43.344859, 10.500328 43.345891, 10.501316 43.347471, 10.502554 43.348176, 10.502286 43.349287, 10.502333 43.349682, 10.502704 43.350209, 10.503318 43.350606, 10.503488 43.350982, 10.503308 43.351209, 10.503726 43.351645, 10.504908 43.352034, 10.505136 43.352788, 10.504805 43.35335, 10.504838 43.353683, 10.505858 43.354534, 10.505572 43.354934, 10.504951 43.355267, 10.504635 43.355973, 10.504468 43.357191, 10.505228 43.358505, 10.50512 43.3591, 10.506578 43.36117, 10.507418 43.361726, 10.508344 43.362732, 10.509047 43.363208, 10.50977 43.361984, 10.511354 43.362151, 10.513629 43.361851, 10.514817 43.362484, 10.516021 43.362756, 10.518772 43.364682, 10.521825 43.364858, 10.524381 43.365932, 10.525139 43.366101, 10.525758 43.366192, 10.527086 43.366004, 10.528612 43.366334, 10.530768 43.368111, 10.531562 43.36828, 10.540814 43.368566, 10.540013 43.367667, 10.53984 43.366661, 10.539363999999999 43.365892))",
"POLYGON ((10.529078 43.276311, 10.520691 43.276896, 10.511281 43.273707, 10.50922 43.278155, 10.50678 43.282915, 10.504904 43.285771, 10.504668 43.286346, 10.50244 43.289586, 10.50209 43.290076, 10.501594 43.290249, 10.501433 43.290495, 10.501552 43.290817, 10.500835 43.29107, 10.500817 43.291457, 10.500077 43.292106, 10.498959 43.291743, 10.49837 43.292921, 10.49813 43.293028, 10.497688 43.292862, 10.497973 43.293088, 10.497883999999999 43.293373, 10.496093 43.295364, 10.495606 43.295528, 10.4947 43.295175, 10.494833 43.295394, 10.494529 43.295889, 10.492646 43.298214, 10.490623 43.299758, 10.490252 43.299771, 10.489886 43.299456, 10.489547 43.300245, 10.48791 43.302, 10.48831 43.30232, 10.48939 43.3027, 10.4954 43.30408, 10.49663 43.30496, 10.49842 43.30727, 10.49981 43.30977, 10.50066 43.31199, 10.50102 43.31255, 10.50168 43.31295, 10.50229 43.31313, 10.50434 43.31315, 10.50647 43.31382, 10.50796 43.31408, 10.511788 43.314196, 10.52064 43.30003, 10.534406 43.277456, 10.529078 43.276311))"]}

foo=pd.DataFrame(coordinates)

I want to know if these two polygons overlap. If they do, I want to make them not overlap, basically slightly changing the coordinates of the first one. The final result would be a df that looks the same but without any overlapping polygons. I tried to use functions from geopandas but I couldn't make it

CodePudding user response:

import pandas as pd
import geopandas as gpd
import shapely.wkt
import itertools

coordinates = {
    "type": ["a", "b"],
    "bound": [
        "POLYGON ((10.539363999999999 43.365892, 10.538892 43.365323, 10.536833 43.363928, 10.536696 43.363371, 10.537307 43.361193, 10.537106 43.360565, 10.534886 43.359623, 10.534587 43.359051, 10.534427 43.358053, 10.533774 43.357612, 10.533429 43.35713, 10.533718 43.356379, 10.534593 43.355854, 10.533964 43.355367, 10.533782 43.354983, 10.534308 43.353337, 10.534263 43.353022, 10.533930999999999 43.352594, 10.533951 43.351423, 10.534102 43.351052, 10.53519 43.350164, 10.535043 43.349716, 10.535639 43.349375, 10.535887 43.348912, 10.535312 43.345687, 10.535032 43.345331, 10.535831 43.344753, 10.535248 43.344131, 10.534851 43.343065, 10.534766 43.34212, 10.533891 43.339719, 10.534133 43.339076, 10.533415 43.338438, 10.533167 43.337918, 10.53318 43.336451, 10.532833 43.335915, 10.53247 43.334263, 10.531761 43.334268, 10.531166 43.332417, 10.530466 43.332039, 10.529987 43.330641, 10.528661 43.330397, 10.528359 43.330185, 10.527991 43.329271, 10.527292 43.328957, 10.526032 43.327416, 10.52335 43.325174, 10.522925 43.325187, 10.522925 43.324842, 10.5215 43.32495, 10.5197 43.32568, 10.51838 43.32592, 10.5157 43.32514, 10.51462 43.32448, 10.51447 43.32393, 10.51453 43.32275, 10.51468 43.32236, 10.5159 43.32081, 10.51646 43.31879, 10.51639 43.31642, 10.51602 43.31557, 10.5156 43.31526, 10.51344 43.31439, 10.51027 43.31409, 10.50796 43.31408, 10.50647 43.31382, 10.50434 43.31315, 10.50229 43.31313, 10.50168 43.31295, 10.50102 43.31255, 10.50066 43.31199, 10.49981 43.30977, 10.49842 43.30727, 10.49663 43.30496, 10.4954 43.30408, 10.49158 43.30329, 10.48831 43.30232, 10.48791 43.302, 10.488399 43.301448, 10.486615 43.302975, 10.484884 43.302534, 10.485042 43.302878, 10.482994 43.304782, 10.482663 43.304724, 10.482886 43.304991, 10.4828 43.305158, 10.481595 43.306412, 10.480434 43.307364, 10.479935 43.307195, 10.480231 43.307488, 10.480127 43.307696, 10.479126 43.3087, 10.47881 43.308672, 10.47885 43.30887, 10.478048 43.309461, 10.477673 43.309304, 10.477878 43.309499, 10.477786 43.309618, 10.476943 43.310227, 10.476225 43.309922, 10.476747 43.310243, 10.476563 43.310584, 10.474496 43.311771, 10.474 43.311494, 10.474228 43.311811, 10.473657 43.312368, 10.473341 43.312548, 10.472824 43.312401, 10.472965 43.312529, 10.472502 43.312977, 10.471031 43.313765, 10.47072 43.313612, 10.470842 43.313809, 10.470249 43.314235, 10.469343 43.314679, 10.469038 43.314637, 10.468523 43.315086, 10.468071 43.315163, 10.467866 43.31504, 10.467517 43.315463, 10.465122 43.316687, 10.463765 43.317708, 10.462973999999999 43.318096, 10.462621 43.318853, 10.462841 43.318868, 10.46278 43.319211, 10.463365 43.31978, 10.466969 43.320409, 10.468894 43.321492, 10.471155 43.32321, 10.472744 43.324135, 10.476031 43.325434, 10.479861 43.326186, 10.48725 43.333303, 10.488329 43.334008, 10.49325 43.339032, 10.495304 43.340752, 10.495874 43.341339, 10.496127 43.342119, 10.496795 43.342705, 10.497031 43.34325, 10.49787 43.343186, 10.498219 43.343415, 10.498161 43.344037, 10.498266 43.344306, 10.498728 43.344525, 10.498637 43.344859, 10.500328 43.345891, 10.501316 43.347471, 10.502554 43.348176, 10.502286 43.349287, 10.502333 43.349682, 10.502704 43.350209, 10.503318 43.350606, 10.503488 43.350982, 10.503308 43.351209, 10.503726 43.351645, 10.504908 43.352034, 10.505136 43.352788, 10.504805 43.35335, 10.504838 43.353683, 10.505858 43.354534, 10.505572 43.354934, 10.504951 43.355267, 10.504635 43.355973, 10.504468 43.357191, 10.505228 43.358505, 10.50512 43.3591, 10.506578 43.36117, 10.507418 43.361726, 10.508344 43.362732, 10.509047 43.363208, 10.50977 43.361984, 10.511354 43.362151, 10.513629 43.361851, 10.514817 43.362484, 10.516021 43.362756, 10.518772 43.364682, 10.521825 43.364858, 10.524381 43.365932, 10.525139 43.366101, 10.525758 43.366192, 10.527086 43.366004, 10.528612 43.366334, 10.530768 43.368111, 10.531562 43.36828, 10.540814 43.368566, 10.540013 43.367667, 10.53984 43.366661, 10.539363999999999 43.365892))",
        "POLYGON ((10.529078 43.276311, 10.520691 43.276896, 10.511281 43.273707, 10.50922 43.278155, 10.50678 43.282915, 10.504904 43.285771, 10.504668 43.286346, 10.50244 43.289586, 10.50209 43.290076, 10.501594 43.290249, 10.501433 43.290495, 10.501552 43.290817, 10.500835 43.29107, 10.500817 43.291457, 10.500077 43.292106, 10.498959 43.291743, 10.49837 43.292921, 10.49813 43.293028, 10.497688 43.292862, 10.497973 43.293088, 10.497883999999999 43.293373, 10.496093 43.295364, 10.495606 43.295528, 10.4947 43.295175, 10.494833 43.295394, 10.494529 43.295889, 10.492646 43.298214, 10.490623 43.299758, 10.490252 43.299771, 10.489886 43.299456, 10.489547 43.300245, 10.48791 43.302, 10.48831 43.30232, 10.48939 43.3027, 10.4954 43.30408, 10.49663 43.30496, 10.49842 43.30727, 10.49981 43.30977, 10.50066 43.31199, 10.50102 43.31255, 10.50168 43.31295, 10.50229 43.31313, 10.50434 43.31315, 10.50647 43.31382, 10.50796 43.31408, 10.511788 43.314196, 10.52064 43.30003, 10.534406 43.277456, 10.529078 43.276311))",
    ],
}

foo = pd.DataFrame(coordinates)

gdf = gpd.GeoDataFrame(
    foo.drop(columns="bound"),
    geometry=foo["bound"].apply(shapely.wkt.loads),
    crs="epsg:4386",
)

for i1,i2 in itertools.combinations(gdf.index, 2):
    if gdf.loc[i1,"geometry"].intersects(gdf.loc[i2,"geometry"]):
        gdf.loc[i2,"geometry"] -= gdf.loc[i1,"geometry"]
        

  • Related