def array_to_raster(inArray, outRst, template, epsg, data_type, noData=None, gisApi='gdal'): """ Send Array to Raster API Available: * gdal; * arcpy """ if gisApi == 'gdal': from osgeo import gdal, osr from gasp.prop.ff import drv_name img_template = gdal.Open(template) geo_transform = img_template.GetGeoTransform() rows, cols = inArray.shape driver = gdal.GetDriverByName(drv_name(outRst)) out = driver.Create(outRst, cols, rows, 1, data_type) out.SetGeoTransform(geo_transform) outBand = out.GetRasterBand(1) if noData: outBand.SetNoDataValue(noData) outBand.WriteArray(inArray) if epsg: from gasp.prop.prj import epsg_to_wkt srs = epsg_to_wkt(epsg) out.SetProjection(srs) outBand.FlushCache() elif gisApi == 'arcpy': import arcpy xmin = arcpy.GetRasterProperties_management(template, "LEFT") cellx = arcpy.GetRasterProperties_management(template, "CELLSIZEX") celly = arcpy.GetRasterProperties_management(template, "CELLSIZEY") new_rst = arcpy.NumPyArrayToRaster(array, float(str(xmin).replace(',', '.')), float(str(cellx).replace(",", ".")), float(str(celly).replace(",", "."))) new_rst.save(outRst) else: raise ValueError('The api {} is not available'.format(gisApi)) return outRst
def publish_raster_layer(layername, datastore, workspace, epsg_code, conf={ 'USER': '******', 'PASSWORD': '******', 'HOST': 'localhost', 'PORT': '8888' }, protocol='http'): """ Publish a Raster layer """ import os import requests from gasp.to.Xml import write_xml_tree from gasp import random_str from gasp.oss.ops import create_folder, del_folder from gasp.prop.prj import epsg_to_wkt url = ('{pro}://{host}:{port}/geoserver/rest/workspaces/{work}/' 'coveragestores/{storename}/coverages').format(host=conf['HOST'], port=conf['PORT'], work=workspace, storename=datastore, pro=protocol) # Create obj with data to be written in the xml xmlTree = { "coverage": { "name": layername, "title": layername, "nativeCRS": str(epsg_to_wkt(epsg_code)), "srs": 'EPSG:{}'.format(str(epsg_code)), } } # Write XML wTmp = create_folder( os.path.join(os.path.dirname(os.path.abspath(__file__)), random_str(7))) xml_file = write_xml_tree(xmlTree, os.path.join(wTmp, 'rst_lyr.xml')) # Create layer with open(xml_file, 'rb') as f: r = requests.post(url, data=f, headers={'content-type': 'text/xml'}, auth=(conf['USER'], conf['PASSWORD'])) del_folder(wTmp) return r
def composite_bnds(rsts, outRst, epsg=None, gisAPI='gdal'): """ Composite Bands API's Available: * gdal; """ if gisAPI == 'gdal': """ Using GDAL """ from osgeo import gdal from gasp.fm.rst import rst_to_array from gasp.prop.ff import drv_name from gasp.prop.rst import rst_dataType, get_nodata # Get Arrays _as = [rst_to_array(r) for r in rsts] # Get nodata values nds = [get_nodata(r, gisApi='gdal') for r in rsts] # Open template and get some metadata img_temp = gdal.Open(rsts[0]) geo_tran = img_temp.GetGeoTransform() band = img_temp.GetRasterBand(1) dataType = rst_dataType(band) rows, cols = _as[0].shape # Create Output drv = gdal.GetDriverByName(drv_name(outRst)) out = drv.Create(outRst, cols, rows, len(_as), dataType) out.SetGeoTransform(geo_tran) if epsg: from gasp.prop.prj import epsg_to_wkt srs = epsg_to_wkt(epsg) out.SetProjection(srs) # Write all bands for i in range(len(_as)): outBand = out.GetRasterBand(i + 1) outBand.SetNoDataValue(nds[i]) outBand.WriteArray(_as[i]) outBand.FlushCache() else: raise ValueError('The api {} is not available'.format(gisAPI)) return outRst
def shp_to_raster(shp, inSource, cellsize, nodata, outRaster, epsg=None, rst_template=None, api='gdal', snap=None): """ Feature Class to Raster cellsize will be ignored if rst_template is defined * API's Available: - gdal; - arcpy; - pygrass; - grass; """ if api == 'gdal': from osgeo import gdal, ogr from gasp.prop.ff import drv_name if not epsg: from gasp.prop.prj import get_shp_sref srs = get_shp_sref(shp).ExportToWkt() else: from gasp.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: 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 gasp.fm.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 == 'arcpy': import arcpy if rst_template: tempEnvironment0 = arcpy.env.extent arcpy.env.extent = template if snap: tempSnap = arcpy.env.snapRaster arcpy.env.snapRaster = snap obj_describe = arcpy.Describe(shp) geom = obj_describe.ShapeType if geom == u'Polygon': arcpy.PolygonToRaster_conversion( shp, inField, outRaster, "CELL_CENTER", "NONE", cellsize ) elif geom == u'Polyline': arcpy.PolylineToRaster_conversion( shp, inField, outRaster, "MAXIMUM_LENGTH", "NONE", cellsize ) if rst_template: arcpy.env.extent = tempEnvironment0 if snap: arcpy.env.snapRaster = tempSnap elif api == 'grass' or api == 'pygrass': """ Vectorial geometry to raster If source is None, the convertion will be based on the cat field. If source is a string, the convertion will be based on the field with a name equal to the given string. If source is a numeric value, all cells of the output raster will have that same value. """ __USE = "cat" if not inSource else "attr" if type(inSource) == str or \ type(inSource) == unicode else "val" if type(inSource) == int or \ type(inSource) == float else None if not __USE: raise ValueError('\'source\' parameter value is not valid') if api == 'pygrass': from grass.pygrass.modules import Module m = Module( "v.to.rast", input=shp, output=outRaster, use=__USE, attribute_column=inSource if __USE == "attr" else None, value=inSource if __USE == "val" else None, overwrite=True, run_=False, quiet=True ) m() else: from gasp import exec_cmd rcmd = exec_cmd(( "v.to.rast input={} output={} use={}{} " "--overwrite --quiet" ).format( shp, outRaster, __USE, "" if __USE == "cat" else " attribute_column={}".format(inSource) \ if __USE == "attr" else " val={}".format(inSource) )) else: raise ValueError('API {} is not available'.format(api)) return outRaster