def crop_raster_by_vector(input_raster, input_polygon, dstnodata="nodata"): # TODO: -wo NUM_THREADS=ALL_CPUS (to use all the CPUs) # TODO: subprocess.call is a better approach output_file = filesystem.tmp_filename("output_", ".tif") cmd = ( "gdalwarp -q -multi -of GTiff -cutline " + input_polygon + " -dstnodata " + dstnodata + " -crop_to_cutline " + input_raster + " " + output_file ) os.system(cmd) return output_file
def create_shp_from_feature(feat, projection=4326): outputFile = filesystem.tmp_filename('shp_', '.shp') # TODO: srs get layer srs = osr.SpatialReference() srs.ImportFromEPSG(projection) # create the new shapefile driver = ogr.GetDriverByName("ESRI Shapefile") outds = driver.CreateDataSource( outputFile ) outLayer = outds.CreateLayer(outputFile, srs) featureDefn = outLayer.GetLayerDefn() outFeature = ogr.Feature(featureDefn) # geometry of the feature geom = feat.GetGeometryRef() outFeature.SetGeometry(geom) outLayer.CreateFeature(outFeature) return outputFile
def crop_raster_by_vector_postgis(input_raster, query=None, db_connection_string=None, dstnodata="nodata"): # TODO: gdalwarp -cutline "PG:host=faostat3.fao.org port=5432 dbname=fenix-spatial user=fenix password=Qwaszx" -csql 'select * from gaul0_3857 where adm0_code=226' -crop_to_cutline -of GTiff -s_srs -dstnodata nodata AB_NDVI_4326.tif somalia3.tif output_file = filesystem.tmp_filename("output_", ".tif") # get db connectin string from configfile (geoserver.json default datastore?) # TODO:handle connection to db # TODO:handle nodata # TODO: -wo NUM_THREADS=ALL_CPUS (to use all the CPUs) # TODO: subprocess.call is a better approach # subprocess.call(['gdalwarp', '-t_srs ' + crs, '-dstnodata 0', '-q', '-cutline ' + mask, '-dstalpha', '-of GTIFF', input, output]). cmd = ( 'gdalwarp -q -multi -of GTiff -cutline "' + db_connection_string + '" -csql "' + query + '" -dstnodata ' + dstnodata + " -crop_to_cutline " + input_raster + " " + output_file ) logger.info("crop_raster_by_vector_postgis: " + cmd) os.system(cmd) return output_file
def create_shapefile_from_json(json_file, projection='EPSG:4326'): output_file = filesystem.tmp_filename('shp_', '.shp') os.system("ogr2ogr -skipfailures -t_srs '" + projection + "' " + output_file + " "+ json_file + " OGRGeoJSON") return output_file