Issue
import geopandas as gpd
sg = gpd.read_file('shapefile1.shp')
sd = gpd.read_file('shapefile2.shp')
kw = sg[sg['region_name'] == 'region1']
tg = sd[sd['region_name'] == 'region2']
region1 and region2 are as below:
mr = kw.geometry.reset_index(drop=True).union(tg.geometry.reset_index(drop=True))
mr.plot()
I can get the image from mr as below:
But, see the boundary of the region
mr.boundary.plot()
I can see the residual lines as the image above.
How remove these residual lines?
I'v tried to remove them using simplify(tolerance=0.01) and buffer(0) functions. But I failed.
I just expect to get outlines of the first image with empty inside.
Solution
For the benefits of the readers, I offer this answer that shows the relevant steps including runnable code that is missing in the question.
import os
os.environ['USE_PYGEOS'] = '0'
import geopandas as gpd
from shapely.geometry import Polygon
# Create 2 geodataframes each containing a simple polygon
polygon_geom1 = Polygon(zip([0, 1.05, 1.05, 0], [-0.1, -0.1, 1.2, 1.2]))
#crs = {'init': 'epsg:4326'}
crs = 'epsg:4326'
polygon1 = gpd.GeoDataFrame(index=[0], crs=crs, geometry=[polygon_geom1])
polygon_geom2 = Polygon(zip([0+1, 1+1, 1+1, 0+1, 0.1+1, 0+1], [0,0,1,1,0.5,0]))
polygon2 = gpd.GeoDataFrame(index=[0], crs=crs, geometry=[polygon_geom2])
# Plot to see them together
ax1 = polygon1.plot(ec='k', color='y')
polygon2.plot(ax=ax1, ec='r', color='lightgray', alpha=0.8)
Next, we do the union of the two geometries:-
polygon1.geometry.values[0].union(polygon2.geometry.values[0])
then, the exterior geometry:-
polygon1.geometry.values[0].union(polygon2.geometry.values[0]).exterior
and, finally, the interior geometry:-
# The interiors of the unioned geometry
# This show item 0 of them
polygon1.geometry.values[0].union(polygon2.geometry.values[0]).interiors[0]
Part 2
This is another example with modified input geometries that will create 2 holes when perform "union" operation.
The code below will show how to create a union-polygon that keeps the large hole in it.
import os
os.environ['USE_PYGEOS'] = '0'
import geopandas as gpd
from shapely.geometry import Polygon
# Create 2 geodataframes each containing a simple polygon
polygon_geom1 = Polygon(zip([0, 1.05, 1.05, 0], [-0.1, -0.1, 1.2, 1.2]))
#crs = {'init': 'epsg:4326'}
crs = 'epsg:4326'
polygon1 = gpd.GeoDataFrame(index=[0], crs=crs, geometry=[polygon_geom1])
polygon_geom2 = Polygon(zip([1, 2, 2, 1,
1.1, 0.95, 1.2 ,
1],
[0,0,1,1,
0.8,0.7,0.4,
0]))
polygon2 = gpd.GeoDataFrame(index=[0], crs=crs, geometry=[polygon_geom2])
# Plot to see them together
ax1 = polygon1.plot(ec='k', color='y')
polygon2.plot(ax=ax1, ec='r', color='lightgray', alpha=0.7)
# The union of the two geometries
# There are 2 holes in it
union_poly = polygon1.geometry.values[0].union(polygon2.geometry.values[0])
union_poly
# The exterior of the unioned geometry
ext_poly = polygon1.geometry.values[0].union(polygon2.geometry.values[0]).exterior
ext_poly
# The interiors of the unioned geometry
# This shows item 0 of them
int_poly0 = polygon1.geometry.values[0].union(polygon2.geometry.values[0]).interiors[0]
int_poly0
# The polygon that contain the big hole only
# (small hole is excluded)
diff_poly = Polygon(ext_poly).difference(Polygon(int_poly0))
diff_poly
Answered By - swatchai
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.