def __init__(self, index_path, point_path): self.data_source = ogr.Open(index_path) self.layer = self.data_source[0] # templates self.path = os.path.join(point_path, 'u{}.laz') self.command = 'las2las -merged -stdout -otxt -i {} -inside {}'
def pol2laz(index_path, point_path, source_path, target_path, attribute): fetcher = Fetcher(index_path=index_path, point_path=point_path) data_source = ogr.Open(source_path) layer = data_source[0] try: os.mkdir(target_path) except OSError: pass for feature in layer: # name try: name = feature[str(attribute)] except ValueError: message = 'No attribute "{}" found in selection datasource.' print(message.format(attribute)) exit() # fetch points geometry = feature.geometry() points = fetcher.fetch(geometry) # save points text = '\n'.join('{} {} {}'.format(x, y, z) for x, y, z in points) laz_path = os.path.join(target_path, name + '.laz') template = 'las2las -stdin -itxt -iparse xyz -o {}' command = template.format(laz_path) process = subprocess.Popen(shlex.split(command), stdin=subprocess.PIPE) process.communicate(text)
def rasterize(feature, source_dir, target_dir): """ Rasterize streamline shape for a single tile into raster. """ geo_transform = get_geo_transform(feature.geometry()) name = feature[str('name')] partial_path = os.path.join(name[:3], name) # target path target_path = os.path.join(target_dir, partial_path) + '.tif' if os.path.exists(target_path): return # create directory try: os.makedirs(os.path.dirname(target_path)) except OSError: pass # no problem # open source source_path = os.path.join(source_dir, partial_path) data_source = ogr.Open(source_path) layer = data_source[0] # create target array kwargs = {'no_data_value': 0, 'geo_transform': geo_transform, 'array': np.zeros((1, 12500, 10000), 'u1'), 'projection': osr.GetUserInputAsWKT(str('epsg:28992'))} with datasets.Dataset(**kwargs) as dataset: for value, attribute in enumerate([2, 3, 4, 4.7], 2): layer.SetAttributeFilter(str('class={}').format(attribute)) gdal.RasterizeLayer(dataset, [1], layer, burn_values=[value]) GTIF.CreateCopy(target_path, dataset, options=OPTIONS)
def command(source_path, **kwargs): """ Rasterize some postgis tables. """ selector = Selector(**kwargs) source_data_source = ogr.Open(source_path) source_layer = source_data_source[0] for feature in source_layer: selector.select(feature)
def constant(index_path, target_dir): """ Create tiles with a constant. """ # properties that are the same for all tiles # open index and loop tiles index_data_source = ogr.Open(index_path) index_layer = index_data_source[0] total = index_layer.GetFeatureCount() gdal.TermProgress_nocb(0) for count, index_feature in enumerate(index_layer, 1): leaf_number = index_feature[str('BLADNR')] target_path = os.path.join( target_dir, leaf_number[:3], leaf_number + '.tif', ) # skip existing if os.path.exists(target_path): gdal.TermProgress_nocb(count / total) continue index_geometry = index_feature.geometry() geo_transform = get_geo_transform(index_geometry) # prepare dataset dataset = DRIVER_GDAL_MEM.Create('', 2000, 2500, 1, DATA_TYPE) dataset.SetProjection(PROJECTION) dataset.SetGeoTransform(geo_transform) # raster band band = dataset.GetRasterBand(1) band.SetNoDataValue(NO_DATA_VALUE) band.Fill(0) try: os.makedirs(os.path.dirname(target_path)) except OSError: pass DRIVER_GDAL_GTIFF.CreateCopy(target_path, dataset, options=OPTIONS) gdal.TermProgress_nocb(count / total)
def reindex(source_path, target_path, size): source_data_source = ogr.Open(source_path) source_layer = source_data_source[0] sr = source_layer.GetSpatialRef() checker = Checker(source_layer) extent = source_layer.GetExtent() target_data_source = driver.CreateDataSource(target_path) target_layer_name = os.path.basename(target_path) target_layer = target_data_source.CreateLayer(target_layer_name, sr) target_layer.CreateField(ogr.FieldDefn(str('name'), ogr.OFTString)) target_layer_defn = target_layer.GetLayerDefn() xcorners = range(int(extent[0]), int(extent[2]), size) ycorners = range(int(extent[3]), int(extent[1]), -size) total = len(ycorners) chars = string.ascii_lowercase ogr.TermProgress_nocb(0) for m, y in enumerate(ycorners): for n, x in enumerate(xcorners): x1, y1, x2, y2 = x, y - size, x + size, y feature = ogr.Feature(target_layer_defn) xname = (chars[n // (26 * 26)] + chars[(n // 26) % 26] + chars[n % 26] + '{:04.0f}'.format(n)) feature[str('name')] = xname ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint_2D(x1, y1) ring.AddPoint_2D(x2, y1) ring.AddPoint_2D(x2, y2) ring.AddPoint_2D(x1, y2) ring.AddPoint_2D(x1, y1) geometry = ogr.Geometry(ogr.wkbPolygon) geometry.AddGeometry(ring) if not checker.intersects(geometry): continue geometry.AssignSpatialReference(sr) feature.SetGeometry(geometry) target_layer.CreateFeature(feature) ogr.TermProgress_nocb((m + 1) / total)
def roof(index_path, point_path, source_path, target_path): fetcher = Fetcher(index_path=index_path, point_path=point_path) data_source = ogr.Open(source_path) layer = data_source[0] try: os.mkdir(target_path) except OSError: pass for char, feature in zip(string.ascii_letters, layer): # if char not in 'mn': # continue geometry = feature.geometry() points = fetcher.fetch(geometry) # classify classes = classify(points) a, b = 0, 255 colors = np.array([[b, a, a], [a, b, a], [a, a, b], [b, a, b]], 'u1')[classes] # save classified cloud text = '\n'.join(parse(points, colors)) laz_path = os.path.join(target_path, char + '.laz') template = 'las2las -stdin -itxt -iparse xyzRGB -o {}' command = template.format(laz_path) process = subprocess.Popen(shlex.split(command), stdin=subprocess.PIPE) process.communicate(text) # save tif tif_path = os.path.join(target_path, char + '.tif') points = points[classes.astype('b1')] kwargs = rasterize(points=points, geometry=geometry) with datasets.Dataset(**kwargs) as dataset: TIF_DRIVER.CreateCopy(tif_path, dataset, options=OPTIONS) print(char, len(points))
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
def command(index_path, source_path, target_dir, attribute): """ Rasterize some postgis tables. """ # investigate sources if source_path.lower().startswith('pg:'): source_data_source = PGDataSource(source_path) else: source_data_source = ogr.Open(source_path) if source_data_source.GetDriver().GetName() == 'ESRI Shapefile': # seems 1.9.1 does not sort, while 1.9.2 does ordered_source_data_source = sorted(source_data_source, key=lambda l: l.GetName()) else: ordered_source_data_source = source_data_source source_field_names = [] for source_layer in ordered_source_data_source: # check attribute for all source layers source_field_names.append( get_field_name(layer=source_layer, attribute=attribute) ) ogr_type = get_ogr_type( data_source=ordered_source_data_source, field_names=source_field_names, ) # Create indexes for shapefiles if necessary if source_data_source.GetDriver().GetName() == 'ESRI Shapefile': for source_layer in ordered_source_data_source: source_layer_name = source_layer.GetName() if os.path.isfile(source_path): source_layer_index_path = source_path[-4:] + '.qix' else: source_layer_index_path = os.path.join( source_path, source_layer_name + '.qix', ) if os.path.exists(source_layer_index_path): continue print('Creating spatial index on {}.'.format(source_layer_name)) source_data_source.ExecuteSQL( str('CREATE SPATIAL INDEX ON {}').format(source_layer_name), ) # rasterize index_data_source = ogr.Open(index_path) index_layer = index_data_source[0] total = index_layer.GetFeatureCount() print('Starting rasterize.') gdal.TermProgress_nocb(0) for count, index_feature in enumerate(index_layer, 1): leaf_number = index_feature[str('BLADNR')] target_path = os.path.join( target_dir, leaf_number[0:3], leaf_number + '.tif', ) if os.path.exists(target_path): gdal.TermProgress_nocb(count / total) continue index_geometry = index_feature.geometry() # prepare dataset data_type = DATA_TYPE[ogr_type] no_data_value = NO_DATA_VALUE[ogr_type] dataset = DRIVER_GDAL_MEM.Create('', 2000, 2500, 1, data_type) dataset.SetProjection(osr.GetUserInputAsWKT(str('epsg:28992'))) dataset.SetGeoTransform(get_geotransform(index_geometry)) band = dataset.GetRasterBand(1) band.SetNoDataValue(no_data_value) band.Fill(no_data_value) burned = False for i, source_layer in enumerate(ordered_source_data_source): source_field_name = source_field_names[i] source_layer.SetSpatialFilter(index_geometry) if not source_layer.GetFeatureCount(): continue # create ogr layer if necessary if hasattr(source_layer, 'as_ogr_layer'): temp_data_source, source_layer = source_layer.as_ogr_layer( name=source_field_name, sr=index_layer.GetSpatialRef(), ) # rasterize gdal.RasterizeLayer( dataset, [1], source_layer, options=['ATTRIBUTE={}'.format(source_field_name)] ) burned = True if burned: leaf_number = index_feature[str('BLADNR')] array = (dataset.ReadAsArray() == 255).astype('u1') if array.any(): # save no data tif for inspection ndv_target_path = os.path.join(target_dir, 'no_data', leaf_number[0:3], leaf_number + '.tif') try: os.makedirs(os.path.dirname(ndv_target_path)) except OSError: pass array.shape = 1, dataset.RasterYSize, dataset.RasterXSize kwargs = { 'array': array, 'no_data_value': 0, 'geo_transform': get_geotransform(index_geometry), 'projection': osr.GetUserInputAsWKT(str('epsg:28992')), } with datasets.Dataset(**kwargs) as ndv_dataset: options = ['compress=deflate', 'tiled=yes'] DRIVER_GDAL_GTIFF.CreateCopy(ndv_target_path, ndv_dataset, options=options) # save try: os.makedirs(os.path.dirname(target_path)) except OSError: pass DRIVER_GDAL_GTIFF.CreateCopy( target_path, dataset, options=['COMPRESS=DEFLATE'], ) gdal.TermProgress_nocb(count / total)