Esempio n. 1
0
    def test_union_no_index(self):
        # explicitly ignore indicies
        dfB = overlay(self.polydf, self.polydf2, how="union", use_sindex=False)
        self.assertEquals(dfB.shape, self.union_shape)

        # remove indicies from df
        self.polydf._sindex = None
        self.polydf2._sindex = None
        dfC = overlay(self.polydf, self.polydf2, how="union")
        self.assertEquals(dfC.shape, self.union_shape)
Esempio n. 2
0
    def test_union_no_index(self):
        # explicitly ignore indicies
        dfB = overlay(self.polydf, self.polydf2, how="union", use_sindex=False)
        self.assertEquals(dfB.shape, self.union_shape)

        # remove indicies from df
        self.polydf._sindex = None
        self.polydf2._sindex = None
        dfC = overlay(self.polydf, self.polydf2, how="union")
        self.assertEquals(dfC.shape, self.union_shape)
Esempio n. 3
0
def merge_precinct_bg(pre_df, bg_df, yr_name):
    """Merge block group boundaries with precinct. (Might take a few minutes.)
    Args: 
        pre_df (geoDataFrame): precinct 
        bg_df (geoDataFrame): block groups
        yr_name (str): year
    Returns: 
        DataFrame: merged block group boundaries and precinct. 
    """

    print('working on intersection for year {}'.format(yr_name))
    newdf = overlay(pre_df, bg_df, how="intersection")
    # intersection has both precinct and block group IDs.
    #newdf.head()
    # create a field with the area, will later divide by the total precinct area
    newdf['intersect_area'] = newdf.geometry.area

    # drop the unneeded columns to clean up
    try:
        cols_to_drop = [
            'BLKGRPCE10', 'COUNTYFP10', 'FUNCSTAT10', 'INTPTLAT10',
            'INTPTLON10', 'MTFCC10', 'NAMELSAD10', 'STATEFP10'
        ]
        newdf = newdf.drop(cols_to_drop, axis=1)
    except ValueError:
        print('cols not present')
    print(newdf.columns)
    print('New df has {} precincts'.format(len(
        pre_df.precname.unique())))  # just checking how many precincts.

    return (newdf)
Esempio n. 4
0
def weights_between_shape_and_xy(shapes, stacked_spatiotemporal):
    x, y = zip(*stacked_spatiotemporal.xy.values)
    grid_gdf = (gpd.GeoSeries(
        gpd.points_from_xy(x=x, y=y, crs=stacked_spatiotemporal.crs),
        crs=shapes.crs).buffer(
            infer_resolution(stacked_spatiotemporal.unstack("xy")) /
            2).envelope)
    index_gdf = gpd.GeoDataFrame(geometry=grid_gdf,
                                 data={"xy": stacked_spatiotemporal.xy},
                                 crs=shapes.crs)
    overlaid = overlay(index_gdf, shapes, how="intersection")
    overlaid["area"] = overlaid.area
    overlaid["weight"] = overlaid.groupby("shape_id").area.transform(
        lambda area: area / area.sum())
    weights = (
        overlaid.set_index(
            ["xy", "shape_id"]).loc[:, "weight"].to_xarray().reindex_like(
                stacked_spatiotemporal)  # add all xy's even without overlay
        .fillna(0)  # xy's without overlay have 0 weight
    )
    return weights
Esempio n. 5
0
 def test_duplicate_column_name(self):
     polydf2r = self.polydf2.rename(columns={"value2": "Shape_Area"})
     df = overlay(self.polydf, polydf2r, how="union")
     self.assertTrue("Shape_Area_2" in df.columns and "Shape_Area" in df.columns)
Esempio n. 6
0
 def test_difference(self):
     df = overlay(self.polydf, self.polydf2, how="difference")
     self.assertEquals(df.shape, (86, 7))
Esempio n. 7
0
 def test_duplicate_column_name(self):
     polydf2r = self.polydf2.rename(columns={'value2': 'Shape_Area'})
     df = overlay(self.polydf, polydf2r, how="union")
     self.assertTrue('Shape_Area_2' in df.columns
                     and 'Shape_Area' in df.columns)
Esempio n. 8
0
 def test_identity(self):
     df = overlay(self.polydf, self.polydf2, how="identity")
     self.assertEquals(df.shape, (154, 7))
Esempio n. 9
0
 def test_symmetric_difference(self):
     df = overlay(self.polydf, self.polydf2, how="symmetric_difference")
     self.assertEquals(df.shape, (122, 7))
Esempio n. 10
0
 def test_union(self):
     df = overlay(self.polydf, self.polydf2, how="union")
     self.assertTrue(type(df) is GeoDataFrame)
     self.assertEquals(df.shape, self.union_shape)
     self.assertTrue('value1' in df.columns and 'Shape_Area' in df.columns)
Esempio n. 11
0
 def test_intersection(self):
     df = overlay(self.polydf, self.polydf2, how="intersection")
     self.assertIsNotNone(df['BoroName'][0])
     self.assertEquals(df.shape, (68, 7))
########################################################################################################################
# filter 1
Gent = data[data['NAAM'] == 'Gent']
print(Gent)
print(Gent.values)
print(Gent.describe())

# filter 2
Grote_gem = data[data['OPPERVL'] > 100000000]

# plot
base = Grote_gem.plot(color='red', edgecolor='none')
Gent.plot(ax=base, color='green', edgecolor='none')
data.plot(ax=base, color='none', edgecolor='grey')
data2.plot(ax=base, color='none', edgecolor='black')
plt.show()

Gent = data[data['NAAM'] == 'Gent']

invloedsfeer = gpd.GeoDataFrame({'geometry': Gent.buffer(distance=20000)
                                 })  # overlay works only for GeoDataFrames
from geopandas.tools import overlay
invloed_bel = overlay(invloedsfeer, data, how='intersection')

base = invloedsfeer.plot(color='yellow')
invloed_bel.plot(ax=base, color='red')
Gent.plot(ax=base, color='none', edgecolor='black')
data2.plot(ax=base, color='none', edgecolor='black')
plt.show()
Esempio n. 13
0
 def test_union(self):
     df = overlay(self.polydf, self.polydf2, how="union")
     self.assertTrue(type(df) is GeoDataFrame)
     self.assertEquals(df.shape, self.union_shape)
     self.assertTrue('value1' in df.columns and 'Shape_Area' in df.columns)
Esempio n. 14
0
 def test_duplicate_column_name(self):
     polydf2r = self.polydf2.rename(columns={'value2': 'Shape_Area'})
     df = overlay(self.polydf, polydf2r, how="union")
     self.assertTrue('Shape_Area_2' in df.columns and 'Shape_Area' in df.columns)
Esempio n. 15
0
 def test_difference(self):
     df = overlay(self.polydf, self.polydf2, how="difference")
     self.assertEquals(df.shape, (86, 7))
Esempio n. 16
0
 def test_symmetric_difference(self):
     df = overlay(self.polydf, self.polydf2, how="symmetric_difference")
     self.assertEquals(df.shape, (122, 7))
Esempio n. 17
0
 def test_identity(self):
     df = overlay(self.polydf, self.polydf2, how="identity")
     self.assertEquals(df.shape, (154, 7))
Esempio n. 18
0
 def test_intersection(self):
     df = overlay(self.polydf, self.polydf2, how="intersection")
     self.assertIsNotNone(df['BoroName'][0])
     self.assertEquals(df.shape, (68, 7))