def upstream(polygon_path, linestring_path, raster_paths, grow, distance, multiplier, separation, path, partial): # open files linestring_features = datasources.PartialDataSource(linestring_path) group = MinimumGroup(raster_paths) target = datasources.TargetDataSource( path=path, template_path=linestring_path, attributes=[KEY], ) # select some or all polygons source = datasources.PartialDataSource(polygon_path) if partial is None: polygon_features = source else: polygon_features = source.select(partial) for polygon_feature in polygon_features: # grow a little polygon = polygon_feature.geometry().Buffer(grow) # query the linestrings for linestring_feature in linestring_features.query(polygon): linestring = linestring_feature.geometry() case = Case(group=group, polygon=polygon, distance=distance, multiplier=multiplier, separation=separation, linestring=linestring) # do points, levels = zip(*list(case.get_levels(False))) if len(levels) > 1: # check upstream index = int(len(levels) / 2) first = levels[:index] last = levels[index:] if sum(first) / len(first) > sum(last) / len(last): # do reverse try: points, levels = zip(*list(case.get_levels(True))) except (TypeError, ValueError): # there are no levels for this case continue # save attributes = dict(linestring_feature.items()) for point, level in zip(points, levels): attributes[KEY] = level target.append(geometry=point, attributes=attributes) return 0
def line_up(source_path, raster_path, target_path, **kwargs): """ Take linestrings from source and create target with height added. Source and target are both shapefiles. """ # target if os.path.exists(target_path): if kwargs.pop('overwrite'): DRIVER.DeleteDataSource(str(target_path)) else: logger.info('"%s" already exists. Use --overwrite.', target_path) return target = DRIVER.CreateDataSource(str(target_path)) # rasters if os.path.isdir(raster_path): datasets = [ gdal.Open(os.path.join(raster_path, path)) for path in sorted(os.listdir(raster_path)) ] else: datasets = [gdal.Open(raster_path)] raster = groups.Group(*datasets) # source source = datasources.PartialDataSource(source_path) process(raster=raster, source=source, target=target, **kwargs)
def bag2tif(index_path, part, **kwargs): """ Rasterize some postgis tables. """ index = datasources.PartialDataSource(index_path) rasterizer = Rasterizer(**kwargs) if part is not None: index = index.select(part) for feature in index: rasterizer.rasterize_region(feature)
def bag2tif(index_path, part, **kwargs): """ Rasterize some postgis tables. """ index = datasources.PartialDataSource(index_path) rasterizer = Rasterizer(**kwargs) if part is not None: index = index.select(part) for count, feature in enumerate(index, 1): rasterizer.rasterize(feature)
def retile(index_path, source_path, target_path, part): """ Convert all features. """ index = datasources.PartialDataSource(index_path) if part is not None: index = index.select(part) retiler = Retiler(source_path=source_path, target_path=target_path) for feature in index: retiler.retile(feature)
def hillshade(index_path, raster_path, output_path, part): """ Convert all features. """ index = datasources.PartialDataSource(index_path) if part is not None: index = index.select(part) calculator = Calculator(raster_path=raster_path, output_path=output_path) for feature in index: calculator.calculate(feature) return 0
def flow_rst(index_path, part, **kwargs): """ """ # select some or all polygons index = datasources.PartialDataSource(index_path) if part is not None: index = index.select(part) for feature in index: rasterize(feature, **kwargs) return 0
def shadow(index_path, raster_path, output_path, part): """ """ index = datasources.PartialDataSource(index_path) if part is not None: index = index.select(part) shadower = Shadower(output_path=output_path, raster_path=raster_path) for feature in index: shadower.shadow(feature) return 0
def fillpits(index_path, part, **kwargs): """ """ # select some or all polygons index = datasources.PartialDataSource(index_path) if part is not None: index = index.select(part) pit_filler = PitFiller(**kwargs) for feature in index: pit_filler.fill(feature) return 0
def aggregate(index_path, part, **kwargs): """ """ # select some or all polygons index = datasources.PartialDataSource(index_path) if part is not None: index = index.select(part) aggregator = Aggregator(**kwargs) for feature in index: aggregator.aggregate(feature) return 0
def flow_vec(index_path, part, **kwargs): """ """ # select some or all polygons index = datasources.PartialDataSource(index_path) if part is not None: index = index.select(part) vectorizer = Vectorizer(**kwargs) for feature in index: vectorizer.vectorize(feature) return 0
def flow_dir(index_path, part, **kwargs): """ """ # select some or all polygons index = datasources.PartialDataSource(index_path) if part is not None: index = index.select(part) calculator = DirectionCalculator(**kwargs) for feature in index: calculator.calculate(feature) return 0
def gmfillnodata(index_path, part, **kwargs): """ Fill no data using the gdal algorithm. """ # select some or all polygons index = datasources.PartialDataSource(index_path) if part is not None: index = index.select(part) gdal_filler = GdalFiller(**kwargs) for feature in index: gdal_filler.fill(feature) return 0
def command(source_path, target_path, raster_paths, statistics, part): """ Main """ # open source datasource source = datasources.PartialDataSource(source_path) if part is not None: source = source.select(part) analyzer = Analyzer(statistics=statistics, raster_paths=raster_paths) # create target datasource target = datasources.TargetDataSource(path=target_path, template_path=source_path, attributes=analyzer.actions) for feature in source: target.append(**analyzer.analyze(feature))
def command(gardens_path, aerial_image_path, target_path, min_green, max_green, check_rast, part, pk): # open input shapefile shape_gardens = ogr.Open(gardens_path) layer_gardens = shape_gardens[0] # check projection of input shapefile sr = layer_gardens.GetSpatialRef() check_sr = get_projection(sr) if check_sr is None: print('[!] ERROR : EPSG projection code missing from shape.') hint = '[*] INFO : Use this command: gdalsrsinfo -o wkt EPSG:28992 > ' print(hint, gardens_path.replace('.shp', '.prj')) return 1 # check spaces in aerial_image_path if ' ' in aerial_image_path: hint = '[!] ERROR : There is a space in the aerial image path: ' print(hint, aerial_image_path) return 1 # check if raster exists if not os.path.isfile(aerial_image_path): hint = '[!] ERROR : The specified file could not be found: ' print(hint, aerial_image_path) return 1 # get raster info and check rasterformat rasterfmt = os.path.splitext(aerial_image_path)[1] if (rasterfmt == '.ecw' or rasterfmt == '.tif' or rasterfmt == '.vrt'): pixelsize, envelope_aerial_image = gdalinfo(aerial_image_path) else: hint = '[!] ERROR : No supported raster format selected: ' print(hint, rasterfmt, ' (use .tif, .vrt or .ecw)') return 1 # delete any existing target driver = ogr.GetDriverByName(str('ESRI Shapefile')) try: driver.DeleteDataSource(str(target_path)) except RuntimeError: pass # prepare target dataset target_shape = driver.CreateDataSource(str(target_path)) target_layer_name = os.path.basename(target_path) target_layer = target_shape.CreateLayer(target_layer_name, sr) wkt = osr.GetUserInputAsWKT(str('EPSG:28992')) open(target_path.replace('.shp', '.prj'), 'w').write(wkt) target_layer_defn = layer_gardens.GetLayerDefn() for i in range(target_layer_defn.GetFieldCount()): target_field_defn = target_layer_defn.GetFieldDefn(i) target_layer.CreateField(target_field_defn) target_field_defn = ogr.FieldDefn(str('green_perc'), ogr.OFTReal) target_field_defn.SetWidth(5) target_field_defn.SetPrecision(3) target_layer.CreateField(target_field_defn) target_field_defn = ogr.FieldDefn(str('green_area'), ogr.OFTReal) target_field_defn.SetWidth(10) target_field_defn.SetPrecision(2) target_layer.CreateField(target_field_defn) # create tmp_dir for files tmp_dirname = os.path.join(os.path.dirname(target_path), 'tmp_greenfactor') if not os.path.exists(tmp_dirname): os.makedirs(tmp_dirname) # initialise progress bar total = layer_gardens.GetFeatureCount() ogr.TermProgress_nocb(0) index = datasources.PartialDataSource(gardens_path) if part is not None: index = index.select(part) # main loop for count, feature_garden in enumerate(index): # get envelope and convert it to the projwin for ecw_gdal_translate geometry_garden = feature_garden.geometry() garden_name = feature_garden.GetFieldAsString(str(pk)) envelope_garden = geometry_garden.GetEnvelope() skip_large_size = check_envelopes_input(garden_name, envelope_garden, envelope_aerial_image) if not skip_large_size == 1: x1, x2, y1, y2 = envelope_garden x1, y1 = np.floor(np.divide((x1, y1), pixelsize)) * pixelsize x2, y2 = np.ceil(np.divide((x2, y2), pixelsize)) * pixelsize envelope_garden_round = (x1, x2, y1, y2) projwin_garden = '%s %s %s %s' % (x1, y2, x2, y1) # create filename clipped aerial image geotiff tmp_aerial_tif = os.path.join(tmp_dirname, garden_name + '.tif') tmp_mask_tif = os.path.join(tmp_dirname, garden_name + '_mask.tif') tmp_green_tif = os.path.join(tmp_dirname, garden_name + '_green.tif') # prepare rgbt-array for green factor calculation rgbt, rasterdata = prepare_aerial_array(tmp_aerial_tif, aerial_image_path, projwin_garden, sr) # prepare mask-array for green factor calculation z = rgbt[:1, :, :] * 0 rasterdata['fillvalue'] = 1 array = create_filled_rasterarray(z, geometry_garden, tmp_mask_tif, rasterdata, print_rast=0) m = array[0].astype('b1') # Call function to determine the green factor result = determine_green_factor(rgbt, m, min_green, max_green) greenfactor, greengarden_percentage, greengarden_pixels = result greengarden_area = greengarden_pixels * pixelsize**2 # Create raster to check green factor if check_rast == 1: rasterdata['fillvalue'] = rasterdata['no_data_value'] = -9999 polygon_envelope_garden = get_envelope_polygon( envelope_garden_round, rasterdata['sr'], ) outside = polygon_envelope_garden.Difference(geometry_garden) create_filled_rasterarray(greenfactor[np.newaxis], outside, tmp_green_tif, rasterdata, print_rast=1) # Create a new feature in shapefile attributes_garden = feature_garden.items() feature = ogr.Feature(target_layer.GetLayerDefn()) feature.SetGeometry(geometry_garden) for key, value in attributes_garden.items(): feature[str(key)] = value feature[str('green_perc')] = greengarden_percentage feature[str('green_area')] = greengarden_area target_layer.CreateFeature(feature) # remove raster os.remove(tmp_aerial_tif) # progress bar ogr.TermProgress_nocb((count + 1) / total) return 0