[1]:
import geopandas
import numpy
import matplotlib.pyplot as plt
import geoplanar
from shapely.geometry import box, Polygon

Violation: Gaps#

[2]:
p1 = box(0,0,10,10)
p2 = Polygon([(10,10), (12,8), (10,6), (12,4), (10,2), (20,5)])

gdf = geopandas.GeoDataFrame(geometry=[p1,p2])
gdf.plot(edgecolor='k')
[2]:
<AxesSubplot:>
_images/gaps_2_1.png
[3]:
geoplanar.gaps(gdf)
[3]:
geometry
2 POLYGON ((10.00000 10.00000, 12.00000 8.00000,...
3 POLYGON ((10.00000 6.00000, 12.00000 4.00000, ...
[4]:
g = geoplanar.gaps(gdf)
[5]:
g.area.values
[5]:
array([4., 4.])
[6]:
gdf1 = geoplanar.fill_gaps(gdf)
[7]:
gdf1.plot(edgecolor='k')
[7]:
<AxesSubplot:>
_images/gaps_7_1.png
[8]:
gdf1.area
[8]:
0    108.0
1     32.0
dtype: float64
[9]:
gdf.area
[9]:
0    100.0
1     32.0
dtype: float64
[10]:
geoplanar.gaps(gdf1)
[10]:
geometry

The default is to merge the gap with the largest neighboring feature.

To merge the gap with the smallest neighboring feature set `Largest=False’:

[11]:
geoplanar.fill_gaps(gdf, largest=False).plot(edgecolor='k')
[11]:
<AxesSubplot:>
_images/gaps_12_1.png
[12]:
geoplanar.fill_gaps(gdf, largest=False).area
[12]:
0    100.0
1     40.0
dtype: float64

Checking edge case#

[13]:
p1 = box(0,0,10,10)
p2 = Polygon([(10,10), (12,8), (10,6), (12,4), (10,2), (20,5)])
p3 = box(17,0,20,2)

gdf = geopandas.GeoDataFrame(geometry=[p1,p2,p3])
gdf.plot(edgecolor='k')
[13]:
<AxesSubplot:>
_images/gaps_15_1.png
[14]:
g = geoplanar.gaps(gdf)
[15]:
g.plot()
[15]:
<AxesSubplot:>
_images/gaps_17_1.png
[16]:
geoplanar.fill_gaps(gdf, largest=False).plot(edgecolor='k')
[16]:
<AxesSubplot:>
_images/gaps_18_1.png
[17]:
geoplanar.fill_gaps(gdf, largest=True).plot(edgecolor='k')
[17]:
<AxesSubplot:>
_images/gaps_19_1.png
[ ]:

[18]:
gdf.plot()
[18]:
<AxesSubplot:>
_images/gaps_21_1.png

Gap with an inlet (non-gap)#

[19]:
p1 = box(0,0,10,10)
p2 = Polygon([(10,10), (12,8), (10,6), (12,4), (11,2), (20,5)])

# a true gap with a inlet
gdf = geopandas.GeoDataFrame(geometry=[p1,p2])
gdf.plot(edgecolor='k')
[19]:
<AxesSubplot:>
_images/gaps_23_1.png
[20]:
geoplanar.gaps(gdf)
[20]:
geometry
2 POLYGON ((10.00000 10.00000, 12.00000 8.00000,...
[21]:
geoplanar.fill_gaps(gdf, largest=False).plot(edgecolor='k')
[21]:
<AxesSubplot:>
_images/gaps_25_1.png

Selective Correction#

[22]:
p1 = box(0,0,10,10)
p2 = Polygon([(10,10), (12,8), (10,6), (12,4), (10,2), (20,5)])
p3 = box(17,0,20,2)

gdf = geopandas.GeoDataFrame(geometry=[p1,p2,p3])
gdf.plot(edgecolor='k')
[22]:
<AxesSubplot:>
_images/gaps_27_1.png
[23]:
gaps = geoplanar.gaps(gdf)
[24]:
base = gdf.plot()
gaps.plot(color='red', ax=base)
[24]:
<AxesSubplot:>
_images/gaps_29_1.png
[25]:
gaps
[25]:
geometry
2 POLYGON ((10.00000 10.00000, 12.00000 8.00000,...
3 POLYGON ((10.00000 6.00000, 12.00000 4.00000, ...
[26]:
g2 = gaps.loc[[2]]
[27]:
g2
[27]:
geometry
2 POLYGON ((10.00000 10.00000, 12.00000 8.00000,...
[28]:
filled = geoplanar.fill_gaps(gdf,g2)
[29]:
base = filled.plot()
g2.plot(color='red', ax=base)
[29]:
<AxesSubplot:>
_images/gaps_34_1.png
[30]:
filled.area
[30]:
0    104.0
1     32.0
2      6.0
dtype: float64
[31]:
filled.shape
[31]:
(3, 1)
[32]:
(filled.area==[104, 32,6]).all()
[32]:
True