def cropSlopeAndNDVI(self): # Open the myDataDownloader = DataHelper.DataDownloader() localfile = myDataDownloader.downloadFiles([config.settings['aoi']]) with fiona.open(localfile, "r") as aoi: geoms = [feature["geometry"] for feature in aoi] self.files['normalized_cropped_urban_slope'] = "output/classified-slope-urban-cropped.tiff" self.files['normalized_cropped_ag_slope'] = "output/classified-slope-AG-cropped.tiff" self.files['classified_cropped_ndvi_file']="output/classified-ndvi-cropped.tiff" print("Cropping Classified Slope and NDVI..") with rasterio.open('output/tmp/classified-slope-urban-normalized.tiff') as urb_src: urb_out_image, urb_out_transform = mask(urb_src, geoms, crop=True) urb_out_meta = urb_src.meta.copy() urb_out_meta.update({"driver": "GTiff", "height": urb_out_image.shape[1], "width": urb_out_image.shape[2], "transform": urb_out_transform}) with rasterio.open(self.files['normalized_cropped_urban_slope'], "w", **urb_out_meta) as urb_dest: urb_dest.write(urb_out_image) with rasterio.open('output/tmp/classified-slope-AG-normalized.tiff') as ag_src: ag_out_image, ag_out_transform = mask(ag_src, geoms, crop=True) ag_out_meta = ag_src.meta.copy() ag_out_meta.update({"driver": "GTiff", "height": ag_out_image.shape[1], "width": ag_out_image.shape[2], "transform": ag_out_transform}) with rasterio.open(self.files['normalized_cropped_ag_slope'], "w", **ag_out_meta) as ag_dest: ag_dest.write(ag_out_image) with rasterio.open('output/tmp/classified-ndvi.tiff') as ndvi_src: ndvi_out_image, ndvi_out_transform = mask(ndvi_src, geoms, crop=True) ndvi_out_meta = ndvi_src.meta.copy() ndvi_out_meta.update({"driver": "GTiff", "height": ndvi_out_image.shape[1], "width": ndvi_out_image.shape[2], "transform": ndvi_out_transform}) with rasterio.open(self.files['classified_cropped_ndvi_file'], "w", **ndvi_out_meta) as ndvi_dest: ndvi_dest.write(ndvi_out_image)
import geopandas from geopandas.tools import sjoin from pysal.esda.mapclassify import Natural_Breaks as nb import numpy as np import DataHelper import os, config cwd = os.getcwd() myDataDownloader = DataHelper.DataDownloader() aoipath = myDataDownloader.downloadFiles([config.settings['aoi']]) riverpath = os.path.join(cwd, config.settings['workingdirectory'], config.settings['rivers']) watershedpath = os.path.join(cwd, config.settings['workingdirectory'], config.settings['watersheds']) outputgeojson = os.path.join(cwd, config.settings['outputdirectory'], 'evals/HYDRO/HYDRO.geojson') river = geopandas.GeoDataFrame.from_file(riverpath) # or geojson etc aoi = geopandas.GeoDataFrame.from_file(aoipath) watershed = geopandas.GeoDataFrame.from_file(watershedpath) watershed['area'] = watershed.apply(lambda row: (row['geometry'].area), axis=1) river['length'] = river.apply(lambda row: (row['geometry'].length), axis=1) watershed_with_rivers = sjoin(watershed, river, how='inner', op='intersects') watershedSumbyRiver = watershed_with_rivers.groupby([ "HYBAS_ID", ]).agg(dict(length="sum")).reset_index() watershed['riverlength'] = watershed.HYBAS_ID.map(
def computeTransportNaturalBreaks(self, rawtransfile): print("Computing Natural breaks on Transport..") myDataDownloader = DataHelper.DataDownloader() localfile = myDataDownloader.downloadFiles([config.settings['aoi']]) with fiona.open(localfile, "r") as aoi: geoms = [feature["geometry"] for feature in aoi] classfiedtranstmppath = os.path.join(self.cwd,config.settings['outputdirectory'],'tmp','classified-transport.tiff') with rasterio.open(rawtransfile) as src: profile = src.profile bands = src.read() for band in bands: b = band[(band != np.array(None)) & (np.logical_not(np.isnan(band))) ] breaks = nb(b.ravel(),k=4,initial=1) bins = breaks.bins.tolist() # bins.insert(1,-1) # add -1 to the beginning of the breaks # print bins print("Writing new Transport with Natural break classes..") with rasterio.open(rawtransfile) as src: profile = src.profile bands = src.read(masked=True) for band in bands: for x in np.nditer(band, op_flags=['readwrite']): x[...] = np.digitize(x,bins) # Reproject and write each band with rasterio.open(classfiedtranstmppath, 'w', **profile) as dst: dst.write(bands) classfiedtranspath = os.path.join(self.cwd,config.settings['outputdirectory'],'classified-transport.tiff') print("Cropping Transport..") with rasterio.open(classfiedtranstmppath) as trans_src: trans_out_image, trans_out_transform = mask(trans_src, geoms, crop=True) trans_out_meta = trans_src.meta.copy() trans_out_meta.update({"driver": "GTiff", "height": trans_out_image.shape[1], "width": trans_out_image.shape[2], "transform": trans_out_transform}) with rasterio.open(classfiedtranspath, "w", **trans_out_meta) as trans_dest: trans_dest.write(trans_out_image) TransClassification = dict([(1,2),(2,3),(3,1),(4,1)]) print("Reclassing Transport file..") finaltransevalpath = os.path.join(self.cwd,config.settings['outputdirectory'],'evals','TRANS', 'TRANS.tiff') with rasterio.open(classfiedtranspath) as transnogdhsrc: classifiedprofile = transnogdhsrc.profile classifiedbands = transnogdhsrc.read() classifiedbands1 = np.vectorize(TransClassification.get)(classifiedbands) classifiedbands2 = classifiedbands1.astype(np.float32) with rasterio.open(finaltransevalpath, 'w', **classifiedprofile) as classifieddst: classifieddst.write(classifiedbands2) print("Reclassing completed") print("...")