def test_non_overlapping_geoms(): """Test that a bounding box returns error if the extents don't overlap""" unit_box = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]) unit_gdf = gpd.GeoDataFrame([1], geometry=[unit_box], crs={'init': 'epsg:4326'}) non_overlapping_gdf = unit_gdf.copy() non_overlapping_gdf = unit_gdf.geometry.apply( lambda x: shapely.affinity.translate(x, xoff=20)) with pytest.raises(ValueError): cl.clip_shp(unit_gdf, non_overlapping_gdf)
def create_labeled_image(latitude, longitude, radius, df, resolution): # Create bounding box and bounds c, bounds = makebox(latitude, longitude, radius) print(latitude, longitude) try: # Clip buildings polygon f_clip = cl.clip_shp(df, c) # Calculate area of clipped buildings divided by area of bounding box area = round(float(sum(f_clip.area) / c.area), 10) except: area = 0.0 print('Building coverage ratio: ' + str(area)) m.fit_bounds(bounds) # Save temp html file from folium delay = 5 fn = 'tempmap.html' tmpurl = 'file://{path}{mapfile}'.format(path=workingdir, mapfile=fn) m.save(fn) # Load map tile and create screenshot browser.get(tmpurl) time.sleep(delay) im = Im.open(BytesIO(browser.get_screenshot_as_png())) os.remove(fn) # Get dimensions and calculate cropping range width, height = im.size left = (width - resolution / 2) / 2 top = (height - resolution / 2) / 2 right = (width + resolution / 2) / 2 bottom = (height + resolution / 2) / 2 # Crop the center of the image im = im.crop((left, top, right, bottom)) ifname = '{}{}_{}_{}_{}.png'.format(traindir, radius, area, latitude, longitude) print(ifname) im.save(ifname)
def test_clip_multipoly(multi_poly_gdf, single_rectangle_gdf): """Test a multi poly object can be clipped properly. Also the bounds of the object should == the bounds of the clip object if they fully overlap (as they do in these fixtures). """ clip = cl.clip_shp(multi_poly_gdf, single_rectangle_gdf) assert hasattr(clip, "geometry") assert np.array_equal(clip.total_bounds, single_rectangle_gdf.total_bounds) # 2 features should be returned with an attribute column assert len(clip.attr) == 2
def test_clip_single_multipolygon( buffered_locations, larger_single_rectangle_gdf ): """Test clipping a multi poly with another poly that no sliver shapes should be returned in this clip. """ multi = buffered_locations.dissolve(by="type").reset_index() clip = cl.clip_shp(multi, larger_single_rectangle_gdf) assert hasattr(clip, "geometry") and clip.geom_type[0] == "Polygon"
def test_clip_multipoint(single_rectangle_gdf, multi_point): """Clipping a multipoint feature with a polygon works as expected. should return a geodataframe with a single multi point feature""" clip = cl.clip_shp(multi_point, single_rectangle_gdf) assert hasattr(clip, "geometry") and clip.geom_type[0] == "MultiPoint" assert hasattr(clip, "attr") # All points should intersect the clip geom assert all(clip.intersects(single_rectangle_gdf.unary_union))
def __clip_data_frame(self, data_frame): """ Clips given data frame. """ clip_extent = self.clip_extent if clip_extent.crs != data_frame.crs: key = str(data_frame.crs) if key in self.projected_clip_extents: clip_extent = self.projected_clip_extents[key] else: clip_extent = clip_extent.to_crs(data_frame.crs) self.projected_clip_extents[key] = clip_extent # requires earthpy version 0.8.0 ! clipped_data_frame = ec.clip_shp(data_frame, clip_extent) clipped_data_frame.crs = data_frame.crs return clipped_data_frame[~clipped_data_frame.is_empty]
def test_clip_lines(): """Test what happens when you give the clip_extent a line GDF.""" clip_line = cl.clip_shp(linez_gdf, poly_in_gdf) assert len(clip_line.geometry) == 2
def test_returns_gdf(): """Test that function returns a GeoDataFrame (or GDF-like) object.""" out = cl.clip_shp(locs_gdf, poly_in_gdf) assert hasattr(out, 'geometry')
try: iso_array.append( gpd.GeoDataFrame.from_features(call.json())) except ValueError: print('Erro ao combinar isométricas. Verifique os dados') concat_area = pd.concat(iso_array, ignore_index=True) concat_area.insert(0, "frn_id", frn_id) concat_area.insert(0, "trading_name", trading_name) concat_area.insert(0, "name", concat_area['value']) concat_area = concat_area.drop(['center'], axis=1) #corta a isométrica combinada com o limite do praça if areafile != 'F': try: print(str(frn_id) + ': Recortando as isométricas com a area') concat_area = cl.clip_shp(concat_area, area[area['area_name'] == region]) except ValueError: print( str(frn_id) + ': ERRO: Não foi possivel recortar com a area. Verifique o nome da área ou se a coordenada está dentro do hub logístico.\n' ) continue else: continue #Recorta as áreas maiores com as áreas menores try: print(str(frn_id) + ': Recortando das isométricas.\n') for i in reversed(range(minValue + 1, maxValue + 1)): concat_area[concat_area['value'] == (i + 1) * 500.] = gpd.overlay(
def main(): # Load data TCEQ_lines = gpd.read_file(os.path.join("..", "Data", "TCEQ_classified.shp")) # streams crs = TCEQ_lines.crs usgs_sites = gpd.read_file(os.path.join('..','Data','usgs_tceq_merged.shp')) # usgs gages # Make extent of master raster into geodataframe for clipping r = rasterio.open(os.path.join('..','Data','hagm_grid','MasterRaster_Houston.tif')) transform = r.transform # raster info on size, how its made, etc. bounds = r.bounds # corners pts = [(bounds.left, bounds.top), (bounds.right, bounds.top), (bounds.right, bounds.bottom), (bounds.left, bounds.bottom)] pts = Polygon(pts) # make the points a polygon shp of raster boundary extent_gdf = gpd.GeoDataFrame({'geometry':[pts]}) extent_gdf.crs = crs # set the new gdf crs equal to the tceq crs ulc, lrc = (bounds.left, bounds.top), (bounds.right, bounds.bottom) # upper left corner, lower right corner # Clip the TCEQ streams to the bounding box gdf of Houston TCEQ_lines = TCEQ_lines.explode() # get rid of multigeometry TCEQ_lines.reset_index(inplace=True, drop=True) TCEQ_lines = clip_shp(TCEQ_lines, extent_gdf) print(TCEQ_lines.head()) print(TCEQ_lines.columns) print(len(TCEQ_lines)) # Make TCEQ clipped shapefile into one big shapefile based on Basin Name - not individual line segments # Clip USGS sites shapefile to the extent of the geodataframe # Assign 'sites' object as each unique site number TCEQ_lines = TCEQ_lines.dissolve('BASIN_NAME') print(len(TCEQ_lines)) usgs_sites = clip_shp(usgs_sites, extent_gdf) # earthpy function sites = usgs_sites['site_no'].unique() # Create a raster the same size/shape as original but with all zeros # Then if values are less than -1000, set them to NaN array = r.read(1) nrow, ncol = array.shape active_mask = np.zeros((nrow, ncol)) active_mask[array <= -1000.] = np.nan # -1000 is set as nan value - change this if not the case # Iterate through years and months to set naming convention (year yyyy month mm) dates = [] start, end = 1989,2009 for y in range(start, end+1): for m in range(12): dates.append(str(y)+'{0:02}'.format(m+1)) # Get volumes for each site iterated through time (89-09) from other script vol_dict = get_USGS_data.USGS_vols_to_dict(sites, start=start, end=end) for yyyymm in vol_dict.keys(): print(yyyymm) final_array = np.zeros((nrow, ncol)) # set a raster to zeros initially for itr, dfrow in TCEQ_lines.iterrows(): x, y = polyline_to_pts(dfrow['geometry']) # get x,y from TCEQ clipped shp and convert to points temp_df = pd.DataFrame({'UTMx':x,'UTMy':y}) # create dataframe using x and y values of the TCEQ clipped shp temp_df['geometry'] = temp_df.apply(lambda i: Point(i['UTMx'], i['UTMy']), axis=1) temp_df = gpd.GeoDataFrame(temp_df, geometry='geometry') temp_df.crs = crs # Spatial join on the sites using geometries to get the site number temp_df = gpd.sjoin(temp_df, usgs_sites[['site_no','geometry']], how='left') temp_df['acre_ft'] = temp_df.apply(lambda i: get_vol(vol_dict ,yyyymm, i['site_no']), axis=1) # temp_df['acre_ft'] = temp_df['acre_ft'].interpolate() if not pd.isnull(temp_df['acre_ft']).all(): ml = skspatial.interp2d(temp_df,'acre_ft', res = 1609.35, ulc = ulc, lrc = lrc) # set up skpatial model object hgrid = ml.points_to_grid() hgrid[np.isnan(hgrid)] = 0 # look at this more! (check out type) final_array += np.array(hgrid) # Set all nan or 0 values to nan in the raster final_array[np.isnan(active_mask)] = np.nan final_array[final_array == 0] = np.nan # sometimes there are relevant zeros - check this out more! # Export dataset (monthly) to raster if not os.path.exists(os.path.join('..','output_rasters')): os.mkdir(os.path.join('..','output_rasters')) new_dataset = rasterio.open(os.path.join('..','output_rasters',f'{yyyymm}.tif'), 'w', driver='GTiff', height = final_array.shape[0], width = final_array.shape[1], count=1, dtype=str(final_array.dtype), crs=crs, transform=transform, nodata= np.nan) new_dataset.write(final_array, 1) new_dataset.close() print(temp_df.head())
def test_returns_gdf(point_gdf, single_rectangle_gdf): """Test that function returns a GeoDataFrame (or GDF-like) object.""" out = cl.clip_shp(point_gdf, single_rectangle_gdf) assert hasattr(out, "geometry")
country_boundary.boundary.plot(ax=ax, color="black") ax.set_title("Major NA Roads Unclipped to US Border", fontsize=20) ax.set_axis_off() plt.show() ############################################################################### # Clip the Data # -------------- # # Now that the data are opened as GeoDataFrame objects and in the same # projection, the data can be clipped! Recall that in this example, the roads # will be clipped to the United States boundary. # # To clip the data, make # sure you put the object to be clipped as the first argument in # ``clip_shp()``, followed by the vector object (boundary) to which you want # the first object clipped. The function will return the clipped GeoDataFrame # of the object that is being clipped (e.g. roads). roads_clipped = ec.clip_shp(roads, country_boundary) # Plot the clipped data # The plot below shows the results of the clip function applied to the roads # sphinx_gallery_thumbnail_number = 5 fig, ax = plt.subplots(figsize=(12, 8)) roads_clipped.plot(ax=ax, color="grey") country_boundary.boundary.plot(ax=ax, color="black") ax.set_title("Major NA Roads Clipped to US Border", fontsize=20) ax.set_axis_off() plt.show()
def test_not_gdf(single_rectangle_gdf): """Non-GeoDataFrame inputs raise attribute errors.""" with pytest.raises(AttributeError): cl.clip_shp((2, 3), single_rectangle_gdf) with pytest.raises(AttributeError): cl.clip_shp(single_rectangle_gdf, (2, 3))
def test_not_gdf(poly_in_gdf): """Non-GeoDataFrame inputs raise attribute errors.""" with pytest.raises(AttributeError): cl.clip_shp((2, 3), poly_in_gdf) with pytest.raises(AttributeError): cl.clip_shp(poly_in_gdf, (2, 3))
def test_clip_points(): """Test clipping a points GDF with a generic polygon geometry.""" clip_pts = cl.clip_shp(locs_gdf, poly_in_gdf) assert len(clip_pts.geometry) == 3 and clip_pts.geom_type[1] == "Point"
def test_clip_multipoly(): """Test that multi poly returns a value error.""" with pytest.raises(ValueError): cl.clip_shp(poly_in_gdf, multi_gdf)
def test_non_overlapping_geoms(): """Test that a bounding box returns error if the extents don't overlap""" with pytest.raises(ValueError): cl.clip_shp(locs_gdf, poly_out_gdf)
def test_clip_lines(two_line_gdf, single_rectangle_gdf): """Test what happens when you give the clip_extent a line GDF.""" clip_line = cl.clip_shp(two_line_gdf, single_rectangle_gdf) assert len(clip_line.geometry) == 2
def test_clip_points(point_gdf, single_rectangle_gdf): """Test clipping a points GDF with a generic polygon geometry.""" clip_pts = cl.clip_shp(point_gdf, single_rectangle_gdf) assert len(clip_pts.geometry) == 3 and clip_pts.geom_type[1] == "Point"
def test_clip_multiline(multi_line, single_rectangle_gdf): """Test that clipping a multiline feature with a poly returns expected output.""" clip = cl.clip_shp(multi_line, single_rectangle_gdf) assert hasattr(clip, "geometry") and clip.geom_type[0] == "MultiLineString"
def test_clip_poly(buffered_locations, single_rectangle_gdf): """Test clipping a polygon GDF with a generic polygon geometry.""" clipped_poly = cl.clip_shp(buffered_locations, single_rectangle_gdf) assert len(clipped_poly.geometry) == 3 assert all(clipped_poly.geom_type == "Polygon")
def check_input_gdfs(): """Test that function fails if not provided with 2 GDFs.""" with pytest.raises(AssertionError): cl.clip_shp(locs, poly_in_gdf) with pytest.raises(AssertionError): cl.clip_shp(poly_in_gdf, locs)
def test_warning_main_clip_function(point_gdf, single_rectangle_gdf): """Check that clip_shp returns a deprecated warning.""" with pytest.raises(Warning, match="clip_shp is deprecated in earthpy"): cl.clip_shp(point_gdf, single_rectangle_gdf)
def test_clip_poly(): """Test clipping a polygon GDF with a generic polygon geometry.""" clipped_poly = cl.clip_shp(locs_buff, poly_in_gdf) assert len(clipped_poly.geometry) == 3 assert clipped_poly.geom_type[1] == "Polygon"
def test_input_gdfs(poly_in_gdf): """Test that function fails if not provided with 2 GDFs.""" with pytest.raises(AttributeError): cl.clip_shp(list(), poly_in_gdf) with pytest.raises(AttributeError): cl.clip_shp(poly_in_gdf, list())
def test_clip_donut_poly(): """Donut holes are multipolygons and should raise ValueErrors.""" with pytest.raises(ValueError): cl.clip_shp(locs_gdf, donut_geom)
def test_warning_main_clip_function(point_gdf, single_rectangle_gdf): with pytest.raises(Warning, match="clip_shp is deprecated in earthpy"): cl.clip_shp(point_gdf, single_rectangle_gdf)