def create_fishnet(boundary, x, y, shpfishnet=None, xy_row_col=None, srs=None, outepsg=None): """ Create a Fishnet """ import os from glass.g.prop.ext import get_ext from glass.g.prop.prj import get_epsg from glass.g.smp.obj import fishnet # Check Path if shpfishnet: if not os.path.exists(os.path.dirname(shpfishnet)): raise ValueError('The path for the output doesn\'t exist') # Get boundary extent xmin, xmax, ymin, ymax = get_ext(boundary, outEpsg=outepsg) # Get SRS epsg = int(outepsg) if outepsg else get_epsg(boundary) if not srs else int( srs) return fishnet((xmin, ymax), (xmax, ymin), x, y, xy_row_col=xy_row_col, epsg=epsg, outfishnet=shpfishnet)
def shp_to_shp(inshp, outshp, gisApi='ogr', supportForSpatialLite=None): """ Convert a vectorial file to another with other file format API's Available: * ogr; * grass; When using gisApi='ogr' - Set supportForSpatialLite to True if outShp is a sqlite db and if you want SpatialLite support for that database. """ if gisApi == 'ogr': from glass.pys import execmd from glass.g.prop import drv_name out_driver = drv_name(outshp) if out_driver == 'SQLite' and supportForSpatialLite: splite = ' -dsco "SPATIALITE=YES"' else: splite = '' cmd = 'ogr2ogr -f "{drv}" {out} {_in}{lite}'.format(drv=out_driver, out=outshp, _in=inshp, lite=splite) # Run command cmdout = execmd(cmd) elif gisApi == 'grass': # TODO identify input geometry type import os from glass.pys.oss import fprop from glass.g.wenv.grs import run_grass from glass.g.prop.prj import get_epsg # Start GRASS GIS Session ws = os.path.dirname(outshp) loc = f'loc_{fprop(outshp, "fn")}' epsg = get_epsg(inshp) gbase = run_grass(ws, location=loc, srs=epsg) import grass.script.setup as gsetup gsetup.init(gbase, ws, loc, 'PERMANENT') from glass.g.it.shp import grs_to_shp, shp_to_grs gshp = shp_to_grs(inshp, fprop(inshp, 'fn')) grs_to_shp(gshp, outshp, 'area') else: raise ValueError('Sorry, API {} is not available'.format(gisApi)) return outshp
def clip(inFeat, clipFeat, outFeat, api_gis="grass", clip_by_region=None): """ Clip Analysis api_gis Options: * grass * pygrass * ogr2ogr """ from glass.pys.oss import fprop if api_gis == "pygrass" or api_gis == "grass": import os from glass.g.wenv.grs import run_grass from glass.g.prop.prj import get_epsg epsg = get_epsg(inFeat) work = os.path.dirname(outFeat) refname = fprop(outFeat, 'fn') loc = f"loc_{refname}" grsbase = run_grass(work, location=loc, srs=epsg) import grass.script.setup as gsetup gsetup.init(grsbase, work, loc, 'PERMANENT') from glass.g.it.shp import shp_to_grs, grs_to_shp from glass.g.prop.feat import feat_count shp = shp_to_grs(inFeat, fprop(inFeat, 'fn')) clp = shp_to_grs(clipFeat, fprop(clipFeat, 'fn')) # Clip rslt = grsclip(shp, clp, refname, cmd=True if api_gis == "grass" else None, clip_by_region=clip_by_region) # Export grs_to_shp(rslt, outFeat, 'area') elif api_gis == 'ogr2ogr': from glass.pys import execmd from glass.g.prop import drv_name rcmd = execmd( ("ogr2ogr -f \"{}\" {} {} -clipsrc {} -clipsrclayer {}").format( drv_name(outFeat), outFeat, inFeat, clipFeat, fprop(clipFeat, 'fn'))) else: raise ValueError("{} is not available!".format(api_gis)) return outFeat
def union(lyrA, lyrB, outShp, api_gis="grass"): """ Calculates the geometric union of the overlayed polygon layers, i.e. the intersection plus the symmetrical difference of layers A and B. API's Available: * saga; * grass; * pygrass; """ if api_gis == "saga": from glass.pys import execmd rcmd = execmd( ("saga_cmd shapes_polygons 17 -A {} -B {} -RESULT {} -SPLIT 1" ).format(lyrA, lyrB, outShp)) elif api_gis == "pygrass" or api_gis == "grass": import os from glass.g.wenv.grs import run_grass from glass.pys.oss import fprop from glass.g.prop.prj import get_epsg ws = os.path.dirname(outShp) refname = fprop(outShp) loc = f"loc_{refname}" gbase = run_grass(ws, location=loc, srs=get_epsg(lyrA)) import grass.script.setup as gs gs.init(gbase, ws, loc, 'PERMANENT') # Import data from glass.g.it.shp import shp_to_grs, grs_to_shp lyr_a = shp_to_grs(lyrA, fprop(lyrA, 'fn'), asCMD=True) lyr_b = shp_to_grs(lyrB, fprop(lyrB, 'fn'), asCMD=True) shpunion = grsunion(lyr_a, lyr_b, refname, cmd=True if api_gis == "grass" else None) # Export data grs_to_shp(shpunion, outShp, "area") else: raise ValueError("{} is not available!".format(api_gis)) return outShp
def get_ext(inFile, outEpsg=None): """ Get Extent of any GIS Data return None if inFile is not a GIS File """ from glass.g.prop import check_isRaster, check_isShp if check_isRaster(inFile): from glass.g.prop.rst import rst_ext extent = rst_ext(inFile) else: if check_isShp(inFile): from glass.g.prop.feat import get_ext as gext extent = gext(inFile) else: return None if outEpsg: from glass.g.prop.prj import get_epsg fileEpsg = get_epsg(inFile) if not fileEpsg: raise ValueError('cannot get EPSG of input file') if fileEpsg != outEpsg: from glass.g.gobj import new_pnt from glass.g.prj.obj import prj_ogrgeom bt_left = prj_ogrgeom(new_pnt(extent[0], extent[2]), fileEpsg, outEpsg, api='ogr' if outEpsg != 4326 else 'shapely') top_right = prj_ogrgeom( new_pnt(extent[1], extent[3]), fileEpsg, outEpsg, api='ogr' if outEpsg != 4326 else 'shapely') left, bottom = bt_left.GetX(), bt_left.GetY() right, top = top_right.GetX(), top_right.GetY() extent = [left, right, bottom, top] return extent
def grscliprst(in_rst, clip_ext, outrst): """ Clip Raster using GRASS GIS """ import os from glass.pys.oss import fprop from glass.g.wenv.grs import run_grass from glass.g.wenv.grs import rst_to_region from glass.g.prop.prj import get_epsg # Get EPSG From Raster EPSG = get_epsg(in_rst) if not EPSG: raise ValueError( 'Cannot get EPSG code of Extent Template File ({})'.format(in_rst)) workspace = os.path.dirname(outrst) loc = 'loc_' + fprop(outrst, 'fn') # Create GRASS GIS Session gbase = run_grass(workspace, location=loc, srs=EPSG) import grass.script.setup as gsetup gsetup.init(gbase, workspace, loc, 'PERMANENT') # GRASS GIS modules from glass.g.it.rst import rst_to_grs, grs_to_rst, grs_to_mask # Add data to GRASS GIS rst = rst_to_grs(in_rst, fprop(in_rst, 'fn'), as_cmd=True) clip = rst_to_grs(clip_ext, fprop(clip_ext, 'fn'), as_cmd=True) # Set New region rst_to_region(clip) # Set Mask grs_to_mask(clip) # Export result return grs_to_rst(rst, outrst)
def fext_to_geof(inF, outF, ocellsize=10): """ Extent of a File to Raster or Shapefile """ from glass.g.prop.ext import get_ext from glass.g.prop import check_isRaster from glass.g.prop.prj import get_epsg # Get extent left, right, bottom, top = get_ext(inF) # Get EPSG of inF EPSG = get_epsg(inF) # Export Boundary isRst = check_isRaster(outF) if isRst: from glass.g.wt.rst import ext_to_rst return ext_to_rst( (left, top), (right, bottom), outF, cellsize=ocellsize, epsg=EPSG, invalidResultAsNull=None ) else: from glass.g.prop import check_isShp isShp = check_isShp(outF) if isShp: return coords_to_boundshp( (left, top), (right, bottom), EPSG, outF ) else: raise ValueError( '{} is not recognized as a file with GeoData'.format( inF ) )
def shp_to_rst(shp, inSource, cellsize, nodata, outRaster, epsg=None, rst_template=None, snapRst=None, api='gdal'): """ Feature Class to Raster cellsize will be ignored if rst_template is defined * API's Available: - gdal; - pygrass; - grass; """ if api == 'gdal': from osgeo import gdal, ogr from glass.g.prop import drv_name if not epsg: from glass.g.prop.prj import get_shp_sref srs = get_shp_sref(shp).ExportToWkt() else: from glass.g.prop.prj import epsg_to_wkt srs = epsg_to_wkt(epsg) # Get Extent dtShp = ogr.GetDriverByName(drv_name(shp)).Open(shp, 0) lyr = dtShp.GetLayer() if not rst_template: if not snapRst: x_min, x_max, y_min, y_max = lyr.GetExtent() x_res = int((x_max - x_min) / cellsize) y_res = int((y_max - y_min) / cellsize) else: from glass.g.prop.rst import adjust_ext_to_snap x_min, y_max, y_res, x_res, cellsize = adjust_ext_to_snap( shp, snapRst) else: from glass.g.rd.rst import rst_to_array img_temp = gdal.Open(rst_template) geo_transform = img_temp.GetGeoTransform() y_res, x_res = rst_to_array(rst_template).shape # Create output dtRst = gdal.GetDriverByName(drv_name(outRaster)).Create( outRaster, x_res, y_res, gdal.GDT_Byte) if not rst_template: dtRst.SetGeoTransform((x_min, cellsize, 0, y_max, 0, -cellsize)) else: dtRst.SetGeoTransform(geo_transform) dtRst.SetProjection(str(srs)) bnd = dtRst.GetRasterBand(1) bnd.SetNoDataValue(nodata) gdal.RasterizeLayer(dtRst, [1], lyr, burn_values=[1]) del lyr dtShp.Destroy() elif api == 'grass' or api == 'pygrass': """ Use GRASS GIS - Start Session - Import data - Convert - Export """ import os from glass.pys.oss import fprop from glass.g.wenv.grs import run_grass from glass.g.prop.prj import get_epsg # Create GRASS GIS Session ws = os.path.dirname(outRaster) loc = fprop(outRaster, 'fn') epsg = get_epsg(shp) gbase = run_grass(ws, location=loc, srs=epsg) import grass.script.setup as gsetup gsetup.init(gbase, ws, loc, 'PERMANENT') # Import Packages from glass.g.it.shp import shp_to_grs from glass.g.it.rst import grs_to_rst from glass.g.wenv.grs import shp_to_region # Shape to GRASS GIS gshp = shp_to_grs(shp, fprop(shp, 'fn'), asCMD=True) # Set Region shp_to_region(gshp, cellsize) # Convert grst = grsshp_to_grsrst(gshp, inSource, gshp + '__rst', api="grass") # Export grs_to_rst(grst, outRaster, as_cmd=True) else: raise ValueError('API {} is not available'.format(api)) return outRaster
def pop_within_area(mapunits, mapunits_id, outcol, subunits, subunits_id, pop_col, mapunits_fk, area_shp, output, res_areas=None, res_areas_fk=None): """ Used to calculate % pop exposta a ruidos superiores a 65db Useful to calculate population a menos de x minutos de um tipo de equipamento Retuns population % living inside some polygons """ import os import pandas as pd from glass.g.rd.shp import shp_to_obj from glass.g.wt.rst import shpext_to_rst from glass.g.wt.shp import obj_to_shp from glass.pys.oss import mkdir, fprop from glass.g.gp.ovl import grsintersection from glass.g.prop.prj import get_epsg from glass.g.wenv.grs import run_grass # Prepare GRASS GIS Workspace configuration oname = fprop(output, 'fn') gw = mkdir(os.path.join(os.path.dirname(output), 'ww_' + oname), overwrite=True) # Boundary to Raster w_epsg = get_epsg(area_shp) ref_rst = shpext_to_rst(mapunits, os.path.join(gw, 'extent.tif'), cellsize=10, epsg=w_epsg) # Create GRASS GIS Session loc = 'loc_' + oname gbase = run_grass(gw, location=loc, srs=ref_rst) import grass.script as grass import grass.script.setup as gsetup gsetup.init(gbase, gw, loc, 'PERMANENT') from glass.g.it.shp import shp_to_grs, grs_to_shp # Send data to GRASS GIS grs_res = shp_to_grs( res_areas if res_areas and res_areas_fk else subunits, fprop(res_areas if res_areas and res_areas_fk else subunits, 'fn'), asCMD=True) grs_ash = shp_to_grs(area_shp, fprop(area_shp, 'fn'), asCMD=True) # Run intersection int_ = grsintersection(grs_res, grs_ash, f'i_{grs_res}_{grs_ash}', api='grass') # Export result res_int = grs_to_shp(int_, os.path.join(gw, int_ + '.shp'), 'area') # Compute new indicator mapunits_df = shp_to_obj(mapunits) subunits_df = shp_to_obj(subunits) if res_areas and res_areas_fk: resareas_df = shp_to_obj(res_areas) int______df = shp_to_obj(res_int) # For each bgri, get hab area with population if res_areas and res_areas_fk: resareas_df['gtarea'] = resareas_df.geometry.area # Group By respop = pd.DataFrame({ 'areav': resareas_df.groupby([res_areas_fk])['gtarea'].agg('sum') }).reset_index() # Join with subunits df respop.rename(columns={res_areas_fk: 'jtblfid'}, inplace=True) subunits_df = subunits_df.merge(respop, how='left', left_on=subunits_id, right_on='jtblfid') subunits_df.drop(['jtblfid'], axis=1, inplace=True) else: subunits_df['areav'] = subunits_df.geometry.area # For each subunit, get area intersecting area_shp int______df['gtarea'] = int______df.geometry.area int_id = 'a_' + res_areas_fk if res_areas and res_areas_fk else \ 'a_' + subunits_id area_int = pd.DataFrame({ 'areai': int______df.groupby([int_id])['gtarea'].agg('sum') }).reset_index() # Join with main subunits df area_int.rename(columns={int_id: 'jtblfid'}, inplace=True) subunits_df = subunits_df.merge(area_int, how='left', left_on=subunits_id, right_on='jtblfid') subunits_df.drop(['jtblfid'], axis=1, inplace=True) subunits_df.areai = subunits_df.areai.fillna(0) subunits_df.areav = subunits_df.areav.fillna(0) subunits_df['pop_af'] = (subunits_df.areai * subunits_df[pop_col]) / subunits_df.areav subunits_pop = pd.DataFrame( subunits_df.groupby([mapunits_fk]).agg({ pop_col: 'sum', 'pop_af': 'sum' })) subunits_pop.reset_index(inplace=True) # Produce final table - mapunits table with new indicator subunits_pop.rename(columns={mapunits_fk: 'jtblid'}, inplace=True) mapunits_df = mapunits_df.merge(subunits_pop, how='left', left_on=mapunits_id, right_on='jtblid') mapunits_df[outcol] = (mapunits_df.pop_af * 100) / mapunits_df[pop_col] mapunits_df.drop(['jtblid', pop_col, 'pop_af'], axis=1, inplace=True) obj_to_shp(mapunits_df, 'geometry', w_epsg, output) return output
def shparea_by_mapunitpopulation(polygons, mapunits, units_id, outcol, output, units_pop=None, areacol=None): """ Polygons area by mapunit or by mapunit population """ import os import pandas as pd from glass.g.wt.rst import shpext_to_rst from glass.pys.oss import mkdir, fprop from glass.g.gp.ovl import grsintersection from glass.g.prop.prj import get_epsg from glass.g.wenv.grs import run_grass from glass.g.rd.shp import shp_to_obj from glass.g.wt.shp import obj_to_shp delareacol = 1 if not areacol else 0 areacol = outcol if not units_pop else areacol if areacol else 'areav' # Prepare GRASS GIS Workspace configuration oname = fprop(output, 'fn') gw = mkdir(os.path.join(os.path.dirname(output), 'ww_' + oname), overwrite=True) # Boundary to raster w_epsg = get_epsg(mapunits) ref_rst = shpext_to_rst(mapunits, os.path.join(gw, 'extent.tif'), cellsize=10, epsg=w_epsg) # Sanitize columns popunits_df_tmp = shp_to_obj(mapunits) drop_cols = [ c for c in popunits_df_tmp.columns.values if c != units_id and c != 'geometry' ] popunits_df_tmp.drop(drop_cols, axis=1, inplace=True) popunits_i = obj_to_shp(popunits_df_tmp, 'geometry', w_epsg, os.path.join(gw, 'popunits.shp')) # Create GRASS GIS Session _l = 'loc_' + oname gbase = run_grass(gw, location=_l, srs=ref_rst) import grass.script as grass import grass.script.setup as gsetup gsetup.init(gbase, gw, _l, 'PERMANENT') from glass.g.it.shp import shp_to_grs, grs_to_shp # Data to GRASS GIS g_popunits = shp_to_grs(popunits_i, fprop(mapunits, 'fn'), asCMD=True) g_polygons = shp_to_grs(polygons, fprop(polygons, 'fn'), asCMD=True) # Run intersection i_shp = grsintersection(g_popunits, g_polygons, f'i_{g_popunits[:5]}_{g_polygons[:5]}', cmd=True) # Export result i_res = grs_to_shp(i_shp, os.path.join(gw, i_shp + '.shp'), 'area') # Open intersection result and mapunits mapunits_df = shp_to_obj(mapunits) int_df = shp_to_obj(i_res) int_df['garea'] = int_df.geometry.area int_gp = pd.DataFrame({ areacol: int_df.groupby(['a_' + units_id])['garea'].agg('sum') }).reset_index() mapunits_df = mapunits_df.merge(int_gp, how='left', left_on=units_id, right_on='a_' + units_id) if units_pop: mapunits_df[outcol] = mapunits_df[areacol] / mapunits_df[units_pop] dc = ['a_' + units_id, areacol ] if units_pop and delareacol else ['a_' + units_id] mapunits_df.drop(dc, axis=1, inplace=True) obj_to_shp(mapunits_df, 'geometry', w_epsg, output) return output
def intersection(inShp, intersectShp, outShp, api='geopandas'): """ Intersection between ESRI Shapefile 'API's Available: * geopandas * saga; * pygrass; * grass; """ if api == 'geopandas': import geopandas from glass.g.rd.shp import shp_to_obj from glass.g.wt.shp import df_to_shp dfShp = shp_to_obj(inShp) dfIntersect = shp_to_obj(intersectShp) res_interse = geopandas.overlay(dfShp, dfIntersect, how='intersection') df_to_shp(res_interse, outShp) elif api == 'saga': from glass.pys import execmd cmdout = execmd( ("saga_cmd shapes_polygons 14 -A {} -B {} -RESULT {} -SPLIT 1" ).format(inShp, intersectShp, outShp)) elif api == 'pygrass' or api == 'grass': import os from glass.g.wenv.grs import run_grass from glass.pys.oss import fprop from glass.g.prop.prj import get_epsg epsg = get_epsg(inShp) w = os.path.dirname(outShp) refname = fprop(outShp, 'fn') loc = f"loc_{refname}" grsbase = run_grass(w, location=loc, srs=epsg) import grass.script.setup as gsetup gsetup.init(grsbase, w, loc, 'PERMANENT') from glass.g.it.shp import shp_to_grs, grs_to_shp shpa = shp_to_grs(inShp, fprop(inShp, 'fn')) shpb = shp_to_grs(intersectShp, fprop(intersectShp, 'fn')) # Intersection intshp = grsintersection(shpa, shpb, refname, True if api == 'grass' else None) # Export r = grs_to_shp(intshp, outShp, 'area') else: raise ValueError("{} is not available!".format(api)) return outShp
def make_dem(grass_workspace, data, field, output, extent_template, method="IDW", cell_size=None, mask=None): """ Create Digital Elevation Model Methods Available: * IDW; * BSPLINE; * SPLINE; * CONTOUR; """ from glass.pys.oss import fprop from glass.g.wenv.grs import run_grass from glass.g.prop.prj import get_epsg LOC_NAME = fprop(data, 'fn', forceLower=True)[:5] + "_loc" # Get EPSG From Raster EPSG = get_epsg(extent_template) if not EPSG: raise ValueError( 'Cannot get EPSG code of Extent Template File ({})'.format( extent_template ) ) # Know if data geometry are points if method == 'BSPLINE' or method == 'SPLINE': from glass.g.prop.feat import get_gtype data_gtype = get_gtype(data, gisApi='ogr') # Create GRASS GIS Location grass_base = run_grass(grass_workspace, location=LOC_NAME, srs=EPSG) # Start GRASS GIS Session import grass.script.setup as gsetup gsetup.init(grass_base, grass_workspace, LOC_NAME, 'PERMANENT') # Get Extent Raster ref_template = ob_ref_rst(extent_template, os.path.join( grass_workspace, LOC_NAME ), cellsize=cell_size) # IMPORT GRASS GIS MODULES # from glass.g.it.rst import rst_to_grs, grs_to_rst from glass.g.it.shp import shp_to_grs from glass.g.wenv.grs import rst_to_region # Configure region rst_to_grs(ref_template, 'extent') rst_to_region('extent') # Convert elevation "data" to GRASS Vector elv = shp_to_grs(data, 'elevation') OUTPUT_NAME = fprop(output, 'fn', forceLower=True) if method == "BSPLINE": from glass.g.rst.itp import bspline # Convert to points if necessary if data_gtype != 'POINT' and data_gtype != 'MULTIPOINT': from glass.g.dp.cg import feat_vertex_to_pnt elev_pnt = feat_vertex_to_pnt(elv, "elev_pnt", nodes=None) else: elev_pnt = elv outRst = bspline(elev_pnt, field, OUTPUT_NAME, mway='bicubic', lyrN=1, asCMD=True) elif method == "SPLINE": from glass.g.rst.itp import surfrst # Convert to points if necessary if data_gtype != 'POINT' and data_gtype != 'MULTIPOINT': from glass.g.dp.cg import feat_vertex_to_pnt elev_pnt = feat_vertex_to_pnt(elv, "elev_pnt", nodes=None) else: elev_pnt = elv outRst = surfrst(elev_pnt, field, OUTPUT_NAME, lyrN=1, ascmd=True) elif method == "CONTOUR": from glass.g.dp.torst import grsshp_to_grsrst as shp_to_rst from glass.g.rst.itp import surfcontour # Apply mask if mask if mask: from glass.g.it.rst import grs_to_mask, rst_to_grs rst_mask = rst_to_grs(mask, 'rst_mask', as_cmd=True) grs_to_mask(rst_mask) # Elevation (GRASS Vector) to Raster elevRst = shp_to_rst(elv, field, 'rst_elevation') # Run Interpolator outRst = surfcontour(elevRst, OUTPUT_NAME, ascmd=True) elif method == "IDW": from glass.g.rst.itp import ridw from glass.g.rst.alg import rstcalc from glass.g.dp.torst import grsshp_to_grsrst as shp_to_rst # Elevation (GRASS Vector) to Raster elevRst = shp_to_rst(elv, field, 'rst_elevation') # Multiply cells values by 100 000.0 rstcalc('int(rst_elevation * 100000)', 'rst_elev_int', api='pygrass') # Run IDW to generate the new DEM ridw('rst_elev_int', 'dem_int', numberPoints=15) # DEM to Float rstcalc('dem_int / 100000.0', OUTPUT_NAME, api='pygrass') # Export DEM to a file outside GRASS Workspace grs_to_rst(OUTPUT_NAME, output) return output
def download_by_boundary(input_boundary, folder_out, osm_name, epsg, GetUrl=True, db_name=None, geomCol=None, justOneFeature=None): """ Download data from OSM using a bounding box """ import os from osgeo import ogr from glass.pys.web import get_file from glass.pys.oss import os_name OS_NAME = os_name() EXTENTS = [] if db_name and geomCol: """ Assuming input_boundary is a PostgreSQL Table """ from glass.pys import obj_to_lst from glass.g.prop.gql import tbl_ext for t in obj_to_lst(input_boundary): EXTENTS.append(tbl_ext(db_name, t, geomCol)) else: if type(input_boundary) == dict: if 'top' in input_boundary and 'bottom' in input_boundary \ and 'left' in input_boundary and 'right' in input_boundary: EXTENTS.append([ input_boundary['left'],input_boundary['right'], input_boundary['bottom'], input_boundary['top'] ]) else: raise ValueError(( 'input_boundary is a dict but the keys are not correct. ' 'Please use left, right, top and bottom as keys' )) elif type(input_boundary) == list: if len(input_boundary) == 4: EXTENTS.append(input_boundary) else: raise ValueError(( 'input boundary is a list with more than 4 objects. ' 'The list should be like: ' 'l = [left, right, bottom, top]' )) elif type(input_boundary) == ogr.Geometry: EXTENTS.append(input_boundary.GetEnvelope()) else: # Assuming input boundary is a file #Check if file exists if not os.path.exists(input_boundary): raise ValueError(( "Sorry, but the file {} does not exist inside the folder {}!" ).format( os.path.basename(input_boundary), os.path.dirname(input_boundary) )) # Check if is a raster from glass.g.prop import check_isRaster isRst = check_isRaster(input_boundary) # Get EPSG if not epsg: from glass.g.prop.prj import get_epsg epsg = get_epsg(input_boundary) if isRst: from glass.g.prop.rst import rst_ext # Get raster extent EXTENTS.append(rst_ext(input_boundary)) else: from glass.g.prop import drv_name # Todo: check if it is shape # Open Dataset inSrc = ogr.GetDriverByName(drv_name( input_boundary)).Open(input_boundary) lyr = inSrc.GetLayer() i = 1 for feat in lyr: geom = feat.GetGeometryRef() featext = geom.GetEnvelope() EXTENTS.append(featext) if justOneFeature: break if epsg != 4326: from glass.g.gobj import new_pnt from glass.g.prj.obj import prj_ogrgeom for i in range(len(EXTENTS)): bottom_left = prj_ogrgeom(new_pnt( EXTENTS[i][0], EXTENTS[i][2]), epsg, 4326) top_right = prj_ogrgeom(new_pnt( EXTENTS[i][1], EXTENTS[i][3]), epsg, 4326) left , bottom = bottom_left.GetX(), bottom_left.GetY() right, top = top_right.GetX() , top_right.GetY() EXTENTS[i] = [left, right, bottom, top] #url = "https://overpass-api.de/api/map?bbox={}" url = "https://lz4.overpass-api.de/api/interpreter?bbox={}" RESULTS = [] for e in range(len(EXTENTS)): bbox_str = ','.join([str(p) for p in EXTENTS[e]]) if GetUrl: RESULTS.append(url.format(bbox_str)) continue if len(EXTENTS) == 1: outOsm = os.path.join(folder_out, osm_name + '.xml') else: outOsm = os.path.join(folder_out, "{}_{}.xml".format(osm_name, str(e))) osm_file = get_file( url.format(bbox_str), outOsm, useWget=None if OS_NAME == 'Windows' else None ) RESULTS.append(osm_file) return RESULTS[0] if len(RESULTS) == 1 else RESULTS
def rsts_to_mosaic(inRasterS, o, api="grass", fformat='.tif'): """ Create Mosaic of Raster """ if api == 'pygrass': """ The GRASS program r.patch allows the user to build a new raster map the size and resolution of the current region by assigning known data values from input raster maps to the cells in this region. This is done by filling in "no data" cells, those that do not yet contain data, contain NULL data, or, optionally contain 0 data, with the data from the first input map. Once this is done the remaining holes are filled in by the next input map, and so on. This program is useful for making a composite raster map layer from two or more adjacent map layers, for filling in "holes" in a raster map layer's data (e.g., in digital elevation data), or for updating an older map layer with more recent data. The current geographic region definition and mask settings are respected. The first name listed in the string input=name,name,name, ... is the name of the first map whose data values will be used to fill in "no data" cells in the current region. The second through last input name maps will be used, in order, to supply data values for for the remaining "no data" cells. """ from grass.pygrass.modules import Module m = Module("r.patch", input=inRasterS, output=o, overwrite=True, run_=False, quiet=True) m() elif api == 'grass': from glass.pys import execmd rcmd = execmd("r.patch input={} output={} --overwrite --quiet".format( ",".join(inRasterS), o)) elif api == 'rasterio': import rasterio from rasterio.merge import merge from glass.g.prop import drv_name from glass.g.prop.prj import get_epsg, epsg_to_wkt if type(inRasterS) != list: from glass.pys.oss import lst_ff rsts = lst_ff(inRasterS, file_format=fformat) else: rsts = inRasterS srcs = [rasterio.open(r) for r in rsts] mosaic, out_trans = merge(srcs) out_meta = srcs[0].meta.copy() out_meta.update({ "driver": drv_name(o), "height": mosaic.shape[1], "width": mosaic.shape[2], "transform": out_trans, "count": 1, "crs": epsg_to_wkt(get_epsg(rsts[0])), "compress": 'lzw' }) with rasterio.open(o, "w", **out_meta) as dest: dest.write(mosaic) else: raise ValueError('api {} is not available'.format(api)) return o