def ob_ref_rst(ref, folder, cellsize=None): """ Get Reference Raster """ from gasp.gt.prop.ff import check_isRaster # Check if refRaster is really a Raster isRst = check_isRaster(ref) if not isRst: from gasp.gt.prop.ff import check_isShp if not check_isShp(ref): raise ValueError( ('Extent Template File has an invalid file format. ' 'Please give a file with one of the following extensions: ' 'shp, gml, json, kml, tif or img')) else: # We have a shapefile # Convert it to Raster from gasp.gt.torst import shp_to_rst ref_rst = shp_to_rst(ref, None, 10 if not cellsize else cellsize, -1, os.path.join(folder, 'ref_raster.tif'), api='gdal') return ref_rst else: return ref
def get_epsg(inFile): """ Get EPSG of any GIS File """ from gasp.gt.prop.ff import check_isRaster, check_isShp if check_isRaster(inFile): return get_rst_epsg(inFile) else: if check_isShp(inFile): return get_epsg_shp(inFile) else: return None
def get_ext(inFile, outEpsg=None): """ Get Extent of any GIS Data return None if inFile is not a GIS File """ from gasp.gt.prop.ff import check_isRaster, check_isShp if check_isRaster(inFile): from gasp.gt.prop.rst import rst_ext extent = rst_ext(inFile) else: if check_isShp(inFile): from gasp.gt.prop.feat import get_ext as gext extent = gext(inFile) else: return None if outEpsg: from gasp.gt.prop.prj import get_epsg fileEpsg = get_epsg(inFile) if not fileEpsg: raise ValueError('cannot get EPSG of input file') if fileEpsg != outEpsg: from gasp.g.to import new_pnt from gasp.g.prj import prj_ogrgeom bt_left = prj_ogrgeom(new_pnt(extent[0], extent[2]), fileEpsg, outEpsg) top_right = prj_ogrgeom(new_pnt(extent[1], extent[3]), fileEpsg, outEpsg) left, bottom = bt_left.GetX(), bt_left.GetY() right, top = top_right.GetX(), top_right.GetY() extent = [left, right, bottom, top] return extent
def fext_to_geof(inF, outF, ocellsize=10): """ Extent of a File to Raster or Shapefile """ from gasp.gt.prop.ext import get_ext from gasp.gt.prop.ff import check_isRaster from gasp.gt.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 gasp.gt.torst import ext_to_rst return ext_to_rst((left, top), (right, bottom), outF, cellsize=ocellsize, epsg=EPSG, invalidResultAsNull=None) else: from gasp.gt.prop.ff import check_isShp isShp = check_isShp(outF) if isShp: from gasp.gt.toshp.coord import coords_to_boundshp return coords_to_boundshp((left, top), (right, bottom), EPSG, outF) else: raise ValueError( '{} is not recognized as a file with GeoData'.format(inF))
def get_ref_raster(refBoundBox, folder, cellsize=None): """ Get Reference Raster """ import os from gasp.gt.prop.ff import check_isRaster # Check if refRaster is really a Raster isRst = check_isRaster(refBoundBox) if not isRst: from gasp.gt.prop.ff import check_isShp if not check_isShp(refBoundBox): raise ValueError(( 'refRaster File has an invalid file format. Please give a file ' 'with one of the following extensions: ' 'shp, gml, json, kml, tif or img' )) else: # We have a shapefile # Check SRS and see if it is a projected SRS from gasp.gt.prop.prj import get_epsg_shp epsg, isProj = get_epsg_shp(refBoundBox, returnIsProj=True) if not epsg: raise ValueError('Cannot get epsg code from {}'.format(refBoundBox)) if not isProj: # A conversion between SRS is needed from gasp.gt.prj import proj ref_shp = proj( refBoundBox, os.path.join(folder, 'tmp_ref_shp.shp'), outEPSG=3857, inEPSG=epsg, gisApi='ogr2ogr' ) epsg = 3857 else: ref_shp = refBoundBox # Convert to Raster from gasp.gt.torst import shp_to_rst refRaster = shp_to_rst( ref_shp, None, 2 if not cellsize else cellsize, -1, os.path.join(folder, 'ref_raster.tif'), api='gdal' ) else: # We have a raster from gasp.gt.prop.prj import get_rst_epsg epsg, isProj = get_rst_epsg(refBoundBox, returnIsProj=True) if not epsg: raise ValueError('Cannot get epsg code from {}'.format(refBoundBox)) # Check if Raster has a SRS with projected coordinates if not isProj: # We need to reproject raster from gasp.gt.prj import reprj_rst refRaster = reprj_rst( refBoundBox, os.path.join(folder, 'refrst_3857.tif'), epsg, 3857 ) epsg = 3857 else: refRaster = refBoundBox return refRaster, epsg
def adjust_ext_to_snap(outExt, snapRst): """ Adjust extent for a output raster to snap with other raster """ from gasp.gt.prop.ff import check_isShp, check_isRaster from gasp.gt.prop.rst import rst_ext, get_cellsize from gasp.g.to 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 gasp.gt.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