def coords_to_boundary(topLeft, lowerRight, epsg, outEpsg=None): """ Top Left and Lower Right to Boundary """ from osgeo import ogr from glass.g.gobj import create_polygon boundary_points = [(topLeft[0], topLeft[1]), (lowerRight[0], topLeft[1]), (lowerRight[0], lowerRight[1]), (topLeft[0], lowerRight[1]), (topLeft[0], topLeft[1])] # Create polygon polygon = create_polygon(boundary_points) # Convert SRS if outEPSG if outEpsg and epsg != outEpsg: from glass.g.prj.obj import prj_ogrgeom poly = prj_ogrgeom(polygon, epsg, outEpsg) return poly else: return polygon
def shpext_to_boundary(in_shp, out_srs=None): """ Read one feature class extent and create a boundary with that extent """ from glass.g.prop.ext import get_ext from glass.g.gobj import create_polygon # Get Extent ext = get_ext(in_shp) # Create points of the new boundary based on the extent boundary_points = [ (ext[0], ext[3]), (ext[1], ext[3]), (ext[1], ext[2]), (ext[0], ext[2]), (ext[0], ext[3]) ] polygon = create_polygon(boundary_points) if out_srs: from glass.g.prop.prj import get_shp_epsg in_srs = get_shp_epsg(in_shp) if in_srs != out_srs: from glass.g.prj.obj import prj_ogrgeom poly = prj_ogrgeom(polygon, in_srs, out_srs, api='shply') return poly else: return polygon else: return polygon
def ext_to_rst(topLeft, btRight, outRst, cellsize=None, epsg=None, outEpsg=None, invalidResultAsNull=None, rstvalue=None): """ Extent to Raster """ import numpy from osgeo import gdal from glass.g.prop import drv_name left, top = topLeft right, bottom = btRight cellsize = 10 if not cellsize else cellsize if outEpsg and epsg and outEpsg != epsg: from glass.g.gobj import new_pnt from glass.g.gobj import create_polygon from glass.g.prj.obj import prj_ogrgeom extGeom = prj_ogrgeom( create_polygon([ new_pnt(left, top), new_pnt(right, top), new_pnt(right, bottom), new_pnt(left, bottom), new_pnt(left, top) ]), epsg, outEpsg) epsg = outEpsg left, right, bottom, top = extGeom.GetEnvelope() # Get row and cols number rows = (float(top) - float(bottom)) / cellsize cols = (float(right) - float(left)) / cellsize rows = int(rows) if rows == int(rows) else int(rows) + 1 cols = int(cols) if cols == int(cols) else int(cols) + 1 if not invalidResultAsNull: if not rstvalue: NEW_RST_ARRAY = numpy.zeros((rows, cols)) else: NEW_RST_ARRAY = numpy.full((rows, cols), rstvalue) else: try: if not rstvalue: NEW_RST_ARRAY = numpy.zeros((rows, cols)) else: NEW_RST_ARRAY = numpy.full((rows, cols), rstvalue) except: return None # Create new Raster img = gdal.GetDriverByName(drv_name(outRst)).Create( outRst, cols, rows, 1, gdal.GDT_Byte) img.SetGeoTransform((left, cellsize, 0, top, 0, -cellsize)) band = img.GetRasterBand(1) band.WriteArray(NEW_RST_ARRAY) if epsg: from osgeo import osr rstSrs = osr.SpatialReference() rstSrs.ImportFromEPSG(epsg) img.SetProjection(rstSrs.ExportToWkt()) band.FlushCache() return outRst
def osm_extraction(boundary, osmdata, output, each_feat=None, epsg=None): """ Extract OSM Data from a xml file with osmosis The extraction is done using the extent of a boundary """ import os from glass.pys import execmd from glass.g.prj.obj import prj_ogrgeom from glass.g.prop import check_isRaster # Check if boundary is a file if os.path.isfile(boundary): # Check if boundary is a raster is_rst = check_isRaster(boundary) if is_rst: # Get Raster EPSG and Extent from glass.g.prop.prj import get_rst_epsg from glass.g.prop.rst import rst_ext from glass.g.gobj import create_polygon in_epsg = get_rst_epsg(boundary) left, right, bottom, top = rst_ext(boundary) boundaries = [ create_polygon([(left, top), (right, top), (right, bottom), (left, bottom), (left, top)]) ] else: # Get Shape EPSG from glass.g.prop.prj import get_shp_epsg in_epsg = get_shp_epsg(boundary) if not each_feat: # Get Shape Extent from glass.g.prop.feat import get_ext from glass.g.gobj import create_polygon left, right, bottom, top = get_ext(boundary) boundaries = [ create_polygon([(left, top), (right, top), (right, bottom), (left, bottom), (left, top)]) ] else: # Get Extent of each feature from osgeo import ogr from glass.g.prop import drv_name src = ogr.GetDriverByName(drv_name(boundary)).Open(boundary) lyr = src.GetLayer() boundaries = [feat.GetGeometryRef() for feat in lyr] else: from glass.g.gobj import wkt_to_geom in_epsg = 4326 if not epsg else epsg if type(boundary) == str: # Assuming it is a WKT string wkt_boundaries = [boundary] elif type(boundary) == list: # Assuming it is a List with WKT strings wkt_boundaries = boundary else: raise ValueError('Given boundary has a not valid value') boundaries = [wkt_to_geom(g) for g in wkt_boundaries] if None in boundaries: raise ValueError( ("boundary parameter is a string, but it is not a valid path " "to a file or a valid WKT string")) # Get output files if len(boundaries) == 1: if os.path.isdir(output): fn, ff = os.path.splitext(os.path.basename(osmdata)) out_files = [os.path.join(output, "ect_{}.{}".format(fn, ff))] else: out_files = [output] else: fn, ff = os.path.splitext(os.path.basename(osmdata)) path = output if os.path.isdir(output) else os.path.dirname(output) out_files = [ os.path.join(path, "ect_{}_{}.{}".format(fn, str(i), ff)) for i in range(len(boundaries)) ] # Extract data using OSMOSIS cmd = ("osmosis --read-{_f} {dtparse}file={_in} " "--bounding-box top={t} left={l} bottom={b} right={r} " "--write-{outext} file={_out}") for g in range(len(boundaries)): # Convert boundary to WGS84 -EPSG 4326 geom_wgs = prj_ogrgeom( boundaries[g], int(in_epsg), 4326, api='shapely') if int(in_epsg) != 4326 else boundaries[g] # Get boundary extent left, right, bottom, top = geom_wgs.GetEnvelope() # Osmosis shell comand osmext = os.path.splitext(osmdata)[1] # Execute command outcmd = execmd( cmd.format( _f='pbf' if osmext == '.pbf' else 'xml', _in=osmdata, t=str(top), l=str(left), b=str(bottom), r=str(right), _out=out_files[g], outext=os.path.splitext(out_files[g])[1][1:], dtparse="" if osmext == '.pbf' else "enableDataParsing=no ")) return output
def adjust_ext_to_snap(outExt, snapRst): """ Adjust extent for a output raster to snap with other raster """ from glass.g.prop import check_isShp, check_isRaster from glass.g.prop.rst import rst_ext, get_cellsize from glass.g.gobj import new_pnt, create_polygon # Check if outExt is a raster or not isRst = check_isRaster(outExt) if isRst: shpAExt = rst_ext(outExt) else: isShp = check_isShp(outExt) if isShp: from glass.g.prop.feat import get_ext shpAExt = get_ext(outExt) else: raise ValueError( ("outExt value should be a path to a SHP or to a Raster file")) # Check if snapRst is a raster isRst = check_isRaster(snapRst) if not isRst: raise ValueError(("snapRst should be a path to a raster file")) # Get snapRst Extent snapRstExt = rst_ext(snapRst) # Get cellsize csize = get_cellsize(snapRst) # Find extent point of outExt inside the two extents # This will be used as pseudo origin snapRstPnt = [ new_pnt(snapRstExt[0], snapRstExt[3]), new_pnt(snapRstExt[1], snapRstExt[3]), new_pnt(snapRstExt[1], snapRstExt[2]), new_pnt(snapRstExt[0], snapRstExt[2]), new_pnt(snapRstExt[0], snapRstExt[3]), ] poly_snap_rst = create_polygon(snapRstPnt) outExtPnt = { 'top_left': new_pnt(shpAExt[0], shpAExt[3]), 'top_right': new_pnt(shpAExt[1], shpAExt[3]), 'bottom_right': new_pnt(shpAExt[1], shpAExt[2]), 'bottom_left': new_pnt(shpAExt[0], shpAExt[2]) } out_rst_pseudo = {} for pnt in outExtPnt: out_rst_pseudo[pnt] = outExtPnt[pnt].Intersects(poly_snap_rst) pseudoOrigin = outExtPnt['top_left'] if out_rst_pseudo['top_left'] else \ outExtPnt['bottom_left'] if out_rst_pseudo['bottom_left'] else \ outExtPnt['top_right'] if out_rst_pseudo['top_right'] else \ outExtPnt['bottom_right'] if out_rst_pseudo['bottom_right'] else None if not pseudoOrigin: raise ValueError(('Extents doesn\'t have overlapping areas')) pseudoOriginName = 'top_left' if out_rst_pseudo['top_left'] else \ 'bottom_left' if out_rst_pseudo['bottom_left'] else \ 'top_right' if out_rst_pseudo['top_right'] else \ 'bottom_right' if out_rst_pseudo['bottom_right'] else None # Get out Raster Shape n_col = int((shpAExt[1] - shpAExt[0]) / csize) n_row = int((shpAExt[3] - shpAExt[2]) / csize) # Get Output Raster real origin/top left yName, xName = pseudoOriginName.split('_') if xName == 'left': # Obtain left of output Raster left_out_rst = snapRstExt[0] + (csize * int( (shpAExt[0] - snapRstExt[0]) / csize)) else: # obtain right of output Raster right_out_rst = snapRstExt[1] - (csize * int( (snapRstExt[1] - shpAExt[1]) / csize)) # Use right to obtain left coordinate left_out_rst = right_out_rst - (n_col * csize) if yName == 'top': # Obtain top of output Raster top_out_rst = snapRstExt[3] - (csize * int( (snapRstExt[3] - shpAExt[3]) / csize)) else: # obtain bottom of output raster bot_out_rst = snapRstExt[2] + (csize * int( (shpAExt[2] - snapRstExt[2]) / csize)) # use bottom to find the top of the output raster top_out_rst = bot_out_rst + (n_row * csize) return left_out_rst, top_out_rst, n_row, n_col, csize