Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_sjoin_values(self):
# GH190
self.polydf.index = [1, 3, 4, 5, 6]
df = sjoin(self.pointdf, self.polydf, how='left')
self.assertEquals(df.shape, (21,8))
df = sjoin(self.polydf, self.pointdf, how='left')
self.assertEquals(df.shape, (12,8))
def test_sjoin_duplicate_column_name(self):
pointdf2 = self.pointdf.rename(columns={'pointattr1': 'Shape_Area'})
df = sjoin(pointdf2, self.polydf, how="left")
self.assertTrue('Shape_Area_left' in df.columns)
self.assertTrue('Shape_Area_right' in df.columns)
def test_sjoin_left(self):
df = sjoin(self.pointdf, self.polydf, how='left')
self.assertEquals(df.shape, (21,8))
for i, row in df.iterrows():
self.assertEquals(row.geometry.type, 'Point')
self.assertTrue('pointattr1' in df.columns)
self.assertTrue('BoroCode' in df.columns)
def test_sjoin_right(self):
# the inverse of left
df = sjoin(self.pointdf, self.polydf, how="right")
df2 = sjoin(self.polydf, self.pointdf, how="left")
self.assertEquals(df.shape, (12, 8))
self.assertEquals(df.shape, df2.shape)
for i, row in df.iterrows():
self.assertEquals(row.geometry.type, 'MultiPolygon')
for i, row in df2.iterrows():
self.assertEquals(row.geometry.type, 'MultiPolygon')
def test_sjoin_right(self):
# the inverse of left
df = sjoin(self.pointdf, self.polydf, how="right")
df2 = sjoin(self.polydf, self.pointdf, how="left")
self.assertEquals(df.shape, (12, 8))
self.assertEquals(df.shape, df2.shape)
for i, row in df.iterrows():
self.assertEquals(row.geometry.type, 'MultiPolygon')
for i, row in df2.iterrows():
self.assertEquals(row.geometry.type, 'MultiPolygon')
def test_sjoin_values(self):
# GH190
self.polydf.index = [1, 3, 4, 5, 6]
df = sjoin(self.pointdf, self.polydf, how='left')
self.assertEquals(df.shape, (21,8))
df = sjoin(self.polydf, self.pointdf, how='left')
self.assertEquals(df.shape, (12,8))
try:
point_poly_join = sjoin(points2, polys, how="left", op="within") # point_poly_join.loc[point_poly_join.FEATUREID > 1]
fld = 'GRIDCODE' #next(str(unicode(x)) for x in polys.columns if x != 'geometry')
except:
polys['link'] = np.nan
point_poly_join = polys #gpd.GeoDataFrame( pd.concat( [points2, polys], ignore_index=True) )
fld = 'link'
# Create group of all points in catchment
grouped = point_poly_join.groupby('FEATUREID')
point_poly_count = grouped[fld].count() # point_poly_count.head() next((x for x in points2.columns if x != 'geometry'),None)
# Join Count column on to NHDCatchments table and keep only 'COMID','CatAreaSqKm','CatCount'
final = polys.join(point_poly_count, on='FEATUREID', lsuffix='_', how='left')
final = final[['FEATUREID', 'AreaSqKM', fld]].fillna(0)
cols = ['COMID', 'CatAreaSqKm%s' % appendMetric, 'CatCount%s' % appendMetric]
if not summaryfield == None: # Summarize fields in list with gpd table including duplicates
point_poly_dups = sjoin(points, polys, how="left", op="within")
grouped2 = point_poly_dups.groupby('FEATUREID')
for x in summaryfield: # Sum the field in summary field list for each catchment
point_poly_stats = grouped2[x].sum()
point_poly_stats.name = x
final = final.join(point_poly_stats, on='FEATUREID', how='left').fillna(0)
cols.append('Cat' + x + appendMetric)
final.columns = cols
# Merge final table with Pct_Full table based on COMID and fill NA's with 0
final = pd.merge(final, pct_full, on='COMID', how='left')
if len(mask_dir) > 0:
if not summaryfield == None:
final.columns = ['COMID','CatAreaSqKmRp100','CatCountRp100']+ ['Cat' + y + appendMetric for y in summaryfield] + ['CatPctFullRp100']
else:
final.columns = ['COMID','CatAreaSqKmRp100','CatCountRp100','CatPctFullRp100']
final['CatPctFull%s' % appendMetric] = final['CatPctFull%s' % appendMetric].fillna(100) # final.head() final.loc[final.CatCount == 0]
#print "elapsed time " + str(dt.now()-startTime)
* 'inner': use intersection of keys from both dfs;
retain only left_df geometry column
op : string, default 'intersection'
One of {'intersects', 'contains', 'within'}.
See http://toblerity.org/shapely/manual.html#binary-predicates.
lsuffix : string, default 'left'
Suffix to apply to overlapping column names
(left GeoDataFrame).
rsuffix : string, default 'right'
Suffix to apply to overlapping column namei
(right GeoDataFrame).
"""
import geopandas as gpd
gdf1 = to_gdf(df1, geom_col=left_geom_col)
gdf2 = to_gdf(df2, geom_col=right_geom_col)
out = gpd.tools.sjoin(gdf1, gdf2, **kwargs)
return to_df(out)