def extract_vectors(vectors, temp_dir, organisation, clip_geom, download=False, bound_error=False): extract_data_failures = [] extract_data_succes = [] bbox = clip_geom.GetEnvelope() wkt = POLYGON.format(x1=bbox[0], x2=bbox[1], y1=bbox[2], y2=bbox[3]) bbox_geom = ogr.CreateGeometryFromWkt(wkt) log_time("info", "Set geoserver connections") gs_dict = set_geoserver_connections(vectors) log_time("info", "Extracting vector data") for vector in vectors: try: log_time("info", "Processing vector:", vector["name"]) log_time("info", "Unique name:", vector["unique_name"]) json_dict = {} subject = get_subject_from_name(vector["layername"], vector["workspace"]) meta_path_exists = False meta_path = os.path.join(temp_dir, vector['unique_name'] + ".json") if os.path.exists(meta_path): log_time("info", "Meta file exists, skipping", subject) meta_path_exists = True continue if not wms_in_extent(vector, bbox_geom) or download: log_time( "info", "Wms layer bbox outside area, retrieving raw data" " or download = True", ) download_vector( vector, temp_dir, meta_path.replace(".json", ".geojson"), *bbox, ) retrieve_sld(vector, gs_dict, meta_path.replace(".json", ".sld")) # log_time("info",'Checking feature count outside geometry') # count = feature_count_outside_geometry(meta_path.replace( # ".json", # ".shp"), # clip_geom) #if count > 0: if bound_error: raise VectorOutsideArea(f"Outside atlas area") except DownloadFailure as e: vector["error"] = "Download failure, message:{}".format(e) extract_data_failures.append(vector) except MissingSLD as e: vector["error"] = "missing sld body layer not in geoserver, {}".format(e) extract_data_failures.append(vector) except VectorOutsideArea as e: vector["error"] = "Vector outside ara, message:{}".format(e) extract_data_failures.append(vector) except RuntimeError as e: vector["error"] = "Vector has extract error {}".format(e) extract_data_failures.append(vector) except AttributeError as e: vector["error"] = "missing sld body layer not in geoserver, {}".format(e) extract_data_failures.append(vector) except json.JSONDecodeError as e: vector["error"] = "Vector has json error{}".format(e) extract_data_failures.append(vector) else: vector["subject"] = subject extract_data_succes.append(vector) finally: if not meta_path_exists: json_dict["atlas"] = vector with open(meta_path, "w") as outfile: json.dump(json_dict, outfile) return extract_data_succes, extract_data_failures
print(b) f=open_File("C:/Users\Hp\Desktop\shp\datia.shp") for a in f: print(a.GetFeatureCount()) if input("press enter"): pass for feat in a: l=[] print(feat.GetField('name')) for k in feat.GetGeometryRef(): for count in range(k.GetPointCount()): l.append([k.GetPoint(count)[1],k.GetPoint(count)[0]]) print(l)''' wkt = "POINT (78.45547199249268 25.660289064684854)" pt = ogr.CreateGeometryFromWkt(wkt) bufferDistance = 0.000500 poly = pt.Buffer(bufferDistance) for k in poly: l = [] for count in range(k.GetPointCount()): l.append([k.GetPoint(count)[1], k.GetPoint(count)[0]]) print(l) a = [[26.2081168673041, 78.1884911730817], [26.207139837723, 78.1888023093275], [26.2067740511394, 78.1889256909421], [26.2067740511394, 78.1889256909421], [26.2069665705372, 78.1900200322202], [26.2069646462726, 78.1900125230691], [26.2070052059809, 78.1919054129465], [26.206721239852, 78.1919536927088], [26.2063939559966, 78.191991243635], [26.2074816899632, 78.1898883917673],
def wkt2region(wkt): return(ogr.CreateGeometryFromWkt(wkt).GetEnvelope())
def dissolve(vector_layer, field=None, simplify=0): """ Dissolved the vector layer into a single multipolygon feature. Value can be filled to used certain field. Parameters ---------- vector_layer : ogr vector value: str Returns ------- out_datasource : ogr datasource """ vector_layer.ResetReading() out_datasource = copymem( vector_layer, layer_name="dissolve", geom_type=vector_layer.GetGeomType(), ) out_layer = out_datasource[0] out_layer_defn = out_layer.GetLayerDefn() if field: unique_dict = {} for fid in range(0, vector_layer.GetFeatureCount()): feature = vector_layer[fid] field_name = feature[field] if not field_name in unique_dict: unique_dict[field_name] = [fid] else: unique_dict[field_name].append(fid) for field_value, fid_list in tqdm(unique_dict.items()): multi = ogr.Geometry(ogr.wkbMultiPolygon) for fid in fid_list: feature = vector_layer[fid] geometry = feature.GetGeometryRef() geometry.CloseRings() wkt = geometry.ExportToWkt() multi.AddGeometryDirectly(ogr.CreateGeometryFromWkt(wkt)) union = multi.UnionCascaded() append_feature(out_layer, out_layer_defn, union, feature.items()) else: multi = ogr.Geometry(ogr.wkbMultiPolygon) for fid in tqdm(range(0, vector_layer.GetFeatureCount())): feature = vector_layer[fid] geometry = feature.geometry() if geometry: if geometry.GetGeometryType() > 3: # multipolygon for single_geom in geometry: single_geom.CloseRings() wkt = single_geom.ExportToWkt() multi.AddGeometryDirectly( ogr.CreateGeometryFromWkt(wkt)) else: geometry.CloseRings() wkt = geometry.ExportToWkt() multi.AddGeometryDirectly(ogr.CreateGeometryFromWkt(wkt)) union = multi.UnionCascaded() append_feature(out_layer, out_layer_defn, union, feature.items()) out_layer = None return out_datasource
def ogr_basic_7(): feat_defn = ogr.FeatureDefn() feat = ogr.Feature(feat_defn) if not feat.Equal(feat): return 'fail' try: feat.SetFieldIntegerList except: return 'skip' feat_clone = feat.Clone() if not feat.Equal(feat_clone): return 'fail' # We MUST delete now as we are changing the feature defn afterwards ! # Crash guaranteed otherwise feat.Destroy() feat_clone.Destroy() field_defn = ogr.FieldDefn('field1', ogr.OFTInteger) feat_defn.AddFieldDefn(field_defn) field_defn = ogr.FieldDefn('field2', ogr.OFTReal) feat_defn.AddFieldDefn(field_defn) field_defn = ogr.FieldDefn('field3', ogr.OFTString) feat_defn.AddFieldDefn(field_defn) field_defn = ogr.FieldDefn('field4', ogr.OFTIntegerList) feat_defn.AddFieldDefn(field_defn) field_defn = ogr.FieldDefn('field5', ogr.OFTRealList) feat_defn.AddFieldDefn(field_defn) field_defn = ogr.FieldDefn('field6', ogr.OFTStringList) feat_defn.AddFieldDefn(field_defn) field_defn = ogr.FieldDefn('field7', ogr.OFTDate) feat_defn.AddFieldDefn(field_defn) field_defn = ogr.FieldDefn('field8', ogr.OFTTime) feat_defn.AddFieldDefn(field_defn) field_defn = ogr.FieldDefn('field9', ogr.OFTDateTime) feat_defn.AddFieldDefn(field_defn) # Cannot test : no binding yet for Binary content #field_defn = ogr.FieldDefn('field10', ogr.OFTBinary) #feat_defn.AddFieldDefn(field_defn) feat = ogr.Feature(feat_defn) feat.SetFID(100) feat.SetField(0, 1) feat.SetField(1, 1.2) feat.SetField(2, "A") feat.SetFieldIntegerList(3, [1, 2]) feat.SetFieldDoubleList(4, [1.2, 3.4]) feat.SetFieldStringList(5, ["A", "B"]) feat.SetField(6, 2010, 1, 8, 22, 48, 15, 4) feat.SetField(7, 2010, 1, 8, 22, 48, 15, 4) feat.SetField(8, 2010, 1, 8, 22, 48, 15, 4) feat_clone = feat.Clone() if not feat.Equal(feat_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() geom = ogr.CreateGeometryFromWkt('POINT(0 1)') feat_almost_clone.SetGeometry(geom) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() geom = ogr.CreateGeometryFromWkt('POINT(0 1)') feat.SetGeometry(geom) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_clone = feat.Clone() if not feat.Equal(feat_clone): feat.DumpReadable() feat_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetFID(99) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetField(0, 2) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetField(1, 2.2) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetField(2, "B") if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetFieldIntegerList(3, [1, 2, 3]) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetFieldIntegerList(3, [1, 3]) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetFieldDoubleList(4, [1.2, 3.4, 5.6]) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetFieldDoubleList(4, [1.2, 3.5]) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetFieldStringList(5, ["A", "B", "C"]) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' feat_almost_clone = feat.Clone() feat_almost_clone.SetFieldStringList(5, ["A", "D"]) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' for num_field in [6, 7, 8]: for i in range(7): feat_almost_clone = feat.Clone() feat_almost_clone.SetField(num_field, 2010 + (i == 0), 1 + (i == 1), 8 + (i == 2), 22 + (i == 3), 48 + (i == 4), 15 + (i == 5), 4 + (i == 6)) if feat.Equal(feat_almost_clone): feat.DumpReadable() feat_almost_clone.DumpReadable() return 'fail' return 'success'
single_point = strata[i] single_age = strata_age[i] single_genus = strata_genus[i] print(single_point) print("age:", single_age) feature = ogr.Feature(layer.GetLayerDefn()) print("t") feature.SetField('UID', ((o) + (i + 1))) print("t") feature.SetField('ID', i + 1) print("t") feature.SetField('TreeS_int', treespeciec_int_strat[k]) feature.SetField('Age_int', single_age) feature.SetField('Genus_int', single_genus) # create wkt wkt = "POINT(%f %f)" % ((((float(single_point[0])) * 10) + UL_x + 5), ((UL_y - ((float(single_point[1])) * 10) - 5))) # Create point from the Well Known Txt point_wkt = ogr.CreateGeometryFromWkt(wkt) feature.SetGeometry(point_wkt) # Create the features layer.CreateFeature(feature) feature = None shapefile.Destroy() ## print("finished")
def add_2d_data(basedir: Path, data_source: ogr.DataSource, ts_source: sqlite3.Connection): """Add 2D grid data points and matching timeseries""" grid_lyr = data_source.GetLayerByName("2D") if ts_source: cur = ts_source.cursor() cur.execute("PRAGMA synchronous = OFF") cur.execute("BEGIN TRANSACTION") east_shp = basedir["east"] vert_shp = basedir["vert"] cal_shp = basedir["cal"] key = 0 with fiona.open(str(east_shp), "r") as east, fiona.open( str(vert_shp), "r") as vert, fiona.open(str(cal_shp), "r") as cal: time_series_dates = np.array(parse_dates(east.schema)) data_source.StartTransaction() with click.progressbar(east, label="Adding gridded data") as east_progress: iterations = 0 # assume that raw and calibrated files are of equal lengths for fe, fv, fc in zip(east_progress, vert, cal): point = ogr.CreateGeometryFromWkt( "POINT({} {})".format(*fe["geometry"]["coordinates"])) feature = ogr.Feature(grid_lyr.GetLayerDefn()) feature.SetField("CODE", fv["properties"]["CODE"]) feature.SetField("VEL_V", fv["properties"]["VEL_V"]) feature.SetField("VEL_STD_V", fv["properties"]["V_STDEV_V"]) feature.SetField("VEL_E", fe["properties"]["VEL_E"]) feature.SetField("VEL_STD_E", fe["properties"]["V_STDEV_E"]) feature.SetField("VEL_V_NOUPLIFT", fc["properties"]["VEL_V"]) feature.SetField("VEL_STD_V_NOUPLIFT", fc["properties"]["V_STDEV_V"]) east_y = np.array([ v for k, v in fe["properties"].items() if k.startswith("D") ]) vert_y = np.array([ v for k, v in fv["properties"].items() if k.startswith("D") ]) cal_y = np.array([ v for k, v in fc["properties"].items() if k.startswith("D") ]) if ts_source: code = fv["properties"]["CODE"] key = key + 1 # Geopackage layers has internal primary key 'fid' that autoincrements # and is therefore the same as this key variable. Saving a bit of space # by not repeating it. sql_code = f"INSERT INTO ts_2d_codes(code) VALUES ('{code}')" cur.execute(sql_code) sql = f"INSERT INTO ts_2d_t(code_key, time, east, vert, vert_cal) VALUES ('{key}', ?, ?, ?, ?)" cur.executemany( sql, zip(time_series_dates, east_y, vert_y, cal_y)) ts_source.commit() feature.SetGeometry(point) feature.SetFID(grid_lyr.GetFeatureCount()) grid_lyr.CreateFeature(feature) feature.Destroy() data_source.CommitTransaction() data_source.SyncToDisk()
def run_mosaic(tile_builder_script, inpath, mosaicname, mosaic_dir, args, pos_arg_keys): if os.path.isfile(inpath): bTextfile = True elif os.path.isdir(inpath): bTextfile = False if not os.path.isdir(mosaic_dir): os.makedirs(mosaic_dir) ## TODO: verify logger woks for both interactive and hpc jobs #### Configure Logger if args.log is not None: logfile = os.path.abspath(args.log) else: logfile = os.path.join(mosaic_dir, default_logfile) lfh = logging.FileHandler(logfile) lfh.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s %(levelname)s- %(message)s', '%m-%d-%Y %H:%M:%S') lfh.setFormatter(formatter) logger.addHandler(lfh) lsh = logging.StreamHandler() lsh.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s %(levelname)s- %(message)s', '%m-%d-%Y %H:%M:%S') lsh.setFormatter(formatter) logger.addHandler(lsh) #### Get exclude list if specified if args.exclude is not None: if not os.path.isfile(args.exclude): logger.error("Value for option --exclude-list is not a valid file") f = open(args.exclude, 'r') exclude_list = set([line.rstrip() for line in f.readlines()]) else: exclude_list = set() #### Get Images #logger.info("Reading input images") xs = [] ys = [] image_list = utils.find_images_with_exclude_list(inpath, bTextfile, mosaic.EXTS, exclude_list) if len(image_list) == 0: raise RuntimeError("No images found in input file or directory: {}".format(inpath)) # remove duplicate images image_list_unique = list(set(image_list)) dupes = [x for n, x in enumerate(image_list) if x in image_list[:n]] if len(dupes) > 0: logger.info("Removed %i duplicate image paths", len(dupes)) logger.debug("Dupes: %s", dupes) image_list = image_list_unique logger.info("%i existing images found", len(image_list)) #### gather image info list logger.info("Getting image info") imginfo_list = [mosaic.ImageInfo(image, "IMAGE") for image in image_list] #### Get mosaic parameters logger.info("Setting mosaic parameters") params = mosaic.getMosaicParameters(imginfo_list[0], args) logger.info("Mosaic parameters: band count=%i, datatype=%s", params.bands, params.datatype) logger.info("Mosaic parameters: projection=%s", params.proj) #### Remove images that do not match ref logger.info("Applying attribute filter") imginfo_list2 = mosaic.filterMatchingImages(imginfo_list, params) if len(imginfo_list2) == 0: raise RuntimeError("No valid images found. Check input filter parameters.") logger.info("%i of %i images match filter", len(imginfo_list2), len(image_list)) #### if extent is specified, build tile params and compare extent to input image geom if args.extent: imginfo_list3 = mosaic.filter_images_by_geometry(imginfo_list2, params) #### else set extent after image geoms computed else: #### Get geom for each image imginfo_list3 = [] for iinfo in imginfo_list2: if iinfo.geom is not None: xs = xs + iinfo.xs ys = ys + iinfo.ys imginfo_list3.append(iinfo) else: # remove from list if no geom logger.debug("Null geometry for image: %s", iinfo.srcfn) params.xmin = min(xs) params.xmax = max(xs) params.ymin = min(ys) params.ymax = max(ys) poly_wkt = 'POLYGON (( {} {}, {} {}, {} {}, {} {}, {} {} ))'.format(params.xmin, params.ymin, params.xmin, params.ymax, params.xmax, params.ymax, params.xmax, params.ymin, params.xmin, params.ymin) params.extent_geom = ogr.CreateGeometryFromWkt(poly_wkt) if len(imginfo_list3) == 0: raise RuntimeError("No images found that intersect mosaic extent") logger.info("Mosaic parameters: resolution %f x %f, tilesize %f x %f, extent %f %f %f %f", params.xres, params.yres, params.xtilesize, params.ytilesize, params.xmin, params.xmax, params.ymin, params.ymax) logger.info("%d of %d input images intersect mosaic extent", len(imginfo_list3), len(imginfo_list2)) ## Sort images by score logger.info("Reading image metadata and determining sort order") for iinfo in imginfo_list3: iinfo.getScore(params) if not args.nosort: imginfo_list3.sort(key=lambda x: x.score) logger.info("Getting Exact Image geometry") imginfo_list4 = [] all_valid = True for iinfo in imginfo_list3: if iinfo.score > 0 or args.nosort: simplify_tolerance = 2.0 * ((params.xres + params.yres) / 2.0) ## 2 * avg(xres, yres), should be 1 for panchromatic mosaics where res = 0.5m geom, xs1, ys1 = mosaic.GetExactTrimmedGeom(iinfo.srcfp, step=args.cutline_step, tolerance=simplify_tolerance) if geom is None: logger.warning("%s: geometry could not be determined, verify image is valid", iinfo.srcfn) all_valid = False elif geom.IsEmpty(): logger.warning("%s: geometry is empty", iinfo.srcfn) all_valid = False else: iinfo.geom = geom tm = datetime.today() imginfo_list4.append(iinfo) centroid = geom.Centroid() logger.info("%s: geometry acquired - centroid: %f, %f", iinfo.srcfn, centroid.GetX(), centroid.GetY()) #print(geom) else: logger.debug("Image has an invalid score: %s --> %i", iinfo.srcfp, iinfo.score) if not all_valid: if not args.allow_invalid_geom: raise RuntimeError("Some source images do not have valid geometries. Cannot proceeed") else: logger.info("--allow-invalid-geom used; mosaic will be created using %i valid images (%i invalid \ images not used.)".format(len(imginfo_list4), len(imginfo_list3)-len(imginfo_list4))) # Get stats if needed logger.info("Getting image metadata") for iinfo in imginfo_list4: logger.info(iinfo.srcfn) if args.calc_stats or args.median_remove: iinfo.get_raster_stats(args.calc_stats, args.median_remove) # Build componenet index if args.component_shp: if args.mode == "ALL" or args.mode == "SHP": contribs = [(iinfo, iinfo.geom) for iinfo in imginfo_list4] logger.info("Number of contributors: %d", len(contribs)) logger.info("Building component index") comp_shp = mosaicname + "_components.shp" if len(contribs) > 0: if os.path.isfile(comp_shp): logger.info("Components shapefile already exists: %s", comp_shp) else: build_shp(contribs, comp_shp, args, params) else: logger.error("No contributing images") # Build cutlines index #### Overlay geoms and remove non-contributors logger.info("Overlaying images to determine contribution geom") contribs = mosaic.determine_contributors(imginfo_list4, params.extent_geom, args.min_contribution_area) logger.info("Number of contributors: %d", len(contribs)) if args.mode == "ALL" or args.mode == "SHP": logger.info("Building cutlines index") shp = mosaicname + "_cutlines.shp" if len(contribs) > 0: if os.path.isfile(shp): logger.info("Cutlines shapefile already exists: %s", shp) else: build_shp(contribs, shp, args, params) else: logger.error("No contributing images") ## Create tile objects tiles = [] xtiledim = math.ceil((params.xmax-params.xmin) / params.xtilesize) ytiledim = math.ceil((params.ymax-params.ymin) / params.ytilesize) logger.info("Tiles: %d rows, %d columns", ytiledim, xtiledim) xtdb = len(str(int(xtiledim))) ytdb = len(str(int(ytiledim))) i = 1 for x in mosaic.drange(params.xmin, params.xmax, params.xtilesize): # Columns if x + params.xtilesize > params.xmax: x2 = params.xmax else: x2 = x + params.xtilesize j = 1 for y in mosaic.drange(params.ymin, params.ymax, params.ytilesize): # Rows if y + params.ytilesize > params.ymax: y2 = params.ymax else: y2 = y + params.ytilesize tilename = "{}_{}_{}.tif".format(mosaicname, mosaic.buffernum(j, ytdb), mosaic.buffernum(i, xtdb)) tile = mosaic.TileParams(x, x2, y, y2, j, i, tilename) tiles.append(tile) j += 1 i += 1 #### Write shapefile of tiles if len(tiles) == 0: raise RuntimeError("No tile objects created") if args.mode == "ALL" or args.mode == "SHP": build_tiles_shp(mosaicname, tiles, params) ## Build tile tasks task_queue = [] #### Create task for each tile arg_keys_to_remove = ( 'l', 'qsubscript', 'parallel_processes', 'log', 'mode', 'extent', 'resolution', 'bands', 'max_cc', 'exclude', 'nosort', 'component_shp', 'cutline_step', 'min_contribution_area', 'calc_stats', 'pbs', 'slurm', 'tday', 'tyear' ) tile_arg_str = taskhandler.convert_optional_args_to_string(args, pos_arg_keys, arg_keys_to_remove) logger.debug("Identifying components of %i subtiles", len(tiles)) i = 0 for t in tiles: logger.debug("Identifying components of tile %i of %i: %s", i, len(tiles), os.path.basename(t.name)) #### determine which images in each tile - create geom and query image geoms logger.debug("Running intersect with imagery") intersects = [] for iinfo, contrib_geom in contribs: if contrib_geom.Intersects(t.geom): if args.median_remove: ## parse median dct into text median_string = ";".join(["{}:{}".format(k, v) for k, v in iinfo.median.items()]) intersects.append("{},{}".format(iinfo.srcfp, median_string)) else: intersects.append(iinfo.srcfp) #### If any images are in the tile, mosaic them if len(intersects) > 0: tile_basename = os.path.basename(os.path.splitext(t.name)[0]) logger.info("Number of contributors to subtile %s: %i", tile_basename, len(intersects)) itpath = os.path.join(mosaic_dir, tile_basename + "_intersects.txt") it = open(itpath, "w") it.write("\n".join(intersects)) it.close() #### Submit QSUB job logger.debug("Building mosaicking job for tile: %s", os.path.basename(t.name)) if not os.path.isfile(t.name): cmd = r'{} {} -e {} {} {} {} -r {} {} -b {} {} {}'.format( tile_builder_script, tile_arg_str, t.xmin, t.xmax, t.ymin, t.ymax, params.xres, params.yres, params.bands, t.name, itpath ) task = taskhandler.Task( 'Tile {0}'.format(os.path.basename(t.name)), 'Mos{:04g}'.format(i), 'python', cmd ) if args.mode == "ALL" or args.mode == "MOSAIC": logger.debug(cmd) task_queue.append(task) else: logger.info("Tile already exists: %s", os.path.basename(t.name)) i += 1 if args.mode == "ALL" or args.mode == "MOSAIC": logger.info("Submitting Tasks") #logger.info(task_queue) if len(task_queue) > 0: try: task_handler = taskhandler.ParallelTaskHandler(args.parallel_processes) except RuntimeError as e: logger.error(e) else: if task_handler.num_processes > 1: logger.info("Number of child processes to spawn: %i", task_handler.num_processes) task_handler.run_tasks(task_queue) logger.info("Done") else: logger.info("No tasks to process")
def ogr_vrt_11(): if gdaltest.vrt_ds is None: return 'skip' f = open('tmp/test.csv', 'wb') f.write('x,val1,y,val2\n'.encode('ascii')) f.write('2,"val11",49,"val12"\n'.encode('ascii')) f.close() try: os.remove('tmp/test.csvt') except: pass vrt_xml = """ <OGRVRTDataSource> <OGRVRTLayer name="test"> <SrcDataSource relativeToVRT="0">tmp/test.csv</SrcDataSource> <SrcLayer>test</SrcLayer> <GeometryField encoding="PointFromColumns" x="x" y="y" reportSrcColumn="false"/> </OGRVRTLayer> </OGRVRTDataSource>""" vrt_ds = ogr.Open(vrt_xml, update=1) vrt_lyr = vrt_ds.GetLayerByName('test') # Only val1 and val2 attributes should be reported if vrt_lyr.GetLayerDefn().GetFieldCount() != 2: return 'fail' if vrt_lyr.GetLayerDefn().GetFieldDefn(0).GetNameRef() != 'val1': return 'fail' if vrt_lyr.GetLayerDefn().GetFieldDefn(1).GetNameRef() != 'val2': return 'fail' feat = ogr.Feature(vrt_lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (3 50)') feat.SetGeometryDirectly(geom) feat.SetField('val1', 'val21') vrt_lyr.CreateFeature(feat) feat.Destroy() vrt_lyr.ResetReading() feat = vrt_lyr.GetFeature(2) geom = feat.GetGeometryRef() if geom.ExportToWkt() != 'POINT (3 50)': return 'fail' if feat.GetFieldAsString('val1') != 'val21': return 'fail' feat.Destroy() # The x and y fields are considered as string by default, so spatial # filter cannot be turned into attribute filter gdal.PushErrorHandler('CPLQuietErrorHandler') vrt_lyr.SetSpatialFilterRect(0, 40, 10, 49.5) ret = vrt_lyr.GetFeatureCount() gdal.PopErrorHandler() if gdal.GetLastErrorMsg().find('not declared as numeric fields') == -1: return 'fail' if ret != 1: return 'fail' vrt_ds.Destroy() vrt_ds = None # Add a .csvt file to specify the x and y columns as reals f = open('tmp/test.csvt', 'wb') f.write('Real,String,Real,String\n'.encode('ascii')) f.close() vrt_ds = ogr.Open(vrt_xml, update=1) vrt_lyr = vrt_ds.GetLayerByName('test') vrt_lyr.SetSpatialFilterRect(0, 40, 10, 49.5) if vrt_lyr.GetFeatureCount() != 1: return 'fail' if gdal.GetLastErrorMsg() != '': return 'fail' vrt_ds.Destroy() vrt_ds = None os.remove('tmp/test.csv') os.remove('tmp/test.csvt') return 'success'
def main(CostSurfacefn, startCoordShp, stopCoordShp, pixelValue, searchdist, WKT_file, closest_trash_dist, newRasterfn, linebuff, Hotspot, rasteradjustment): #G = ox.graph_from_place('Willemsparkbuurt, Netherlands', network_type='walk') costSurfaceArray, noDataValue = raster2array( CostSurfacefn) # creates array from cost surface raster #get coordinates out of shp HOUSEHOLDS file_house = ogr.Open(startCoordShp) shape_house = file_house.GetLayer(0) #get coordinates out of shp TrashCan) file_trash = ogr.Open(stopCoordShp) shape_trash = file_trash.GetLayer(0) #open hotspot file and put into list file_hotspot = ogr.Open(Hotspot) shape_hotspot = file_hotspot.GetLayer(0) hotspotlist = [] for i in range(shape_hotspot.GetFeatureCount()): feature_hotspot = shape_hotspot.GetFeature(i) feature_json_hotspot = feature_hotspot.ExportToJson() feature_json_hotspot = eval(feature_json_hotspot) hotcoordX = feature_json_hotspot['geometry']['coordinates'][0] hotcoordY = feature_json_hotspot['geometry']['coordinates'][1] hotCoord = (hotcoordX, hotcoordY) hotspotlist.append(hotCoord) #errorfix null = None with open(WKT_file, 'w') as f: #write first line of WKT file f.write( str('Geometry') + '\t' + 'Identifica_house' + '\t' + 'length' + '\t' + 'LOCATIENR' + '\t' + 'geom_pap' + '\t' + 'LOCATIE_pap' + '\t' + 'geom_glas' + '\t' + 'LOCATIE_glas' + '\t' + 'geom_plas' + '\t' + 'LOCATIE_plas' + '\n') already_made = {} #every household #for i in range(shape_house.GetFeatureCount()): for i in range(1): feature_house = shape_house.GetFeature(i) feature_json_house = feature_house.ExportToJson() feature_json_house = eval(feature_json_house) startcoordX = feature_json_house['geometry']['coordinates'][0] startcoordY = feature_json_house['geometry']['coordinates'][1] startCoord = (startcoordX, startcoordY) #to make sure that the costsurface is reset every time costSurfaceArray2 = costSurfaceArray """find the closest hotspot area, for now, its this""" """with in certain distances make a list of closest hotspots""" hotspotdistancedict = {} small = 999999 for hotspot in hotspotlist: distance = distanceR2(startcoordX, startcoordY, hotspot[0], hotspot[1]) hotspotdistancedict[distance] = hotspot if distance < small: small = distance hotspotxy = hotspotdistancedict[small] housexy = (startcoordX, startcoordY) #shortestpath_wkt = shortestpathtohotspot (G, housexy, hotspotxy, node_buff, edge_buff) direction_wkt = directiontohotspot(housexy, hotspotxy, linebuff) direction_geojson = WKT2GEOJSON(str(direction_wkt)) hotspotinraster = Bresenham_with_rasterio(CostSurfacefn, direction_geojson, rasteradjustment) """This is where the 'preference' is added ot the regular ] raster, this is done for every household""" costandhotspotarray = costSurfaceArray2 * hotspotinraster #array2raster(CostSurfacefn,newRasterfn,costandhotspotarray) considerable_trashcans = [] #every trashcan # if point in polygon which is already calculated, use that one. if feature_json_house['properties']['id_pand'] in already_made: print('==========duplicate found=========') lists = already_made[feature_json_house['properties'] ['id_pand']] f.write( str(lists[1]) + '\t' + str(lists[2]) + '\t' + str(lists[0]) + '\t' + str(lists[3]) + '\t' + str(lists[4]) + '\t' + str(lists[5]) + '\t' + str(lists[6]) + '\t' + str(lists[7]) + '\t' + str(lists[8]) + '\t' + str(lists[9]) + '\n') else: for i in range(shape_trash.GetFeatureCount()): feature_trash = shape_trash.GetFeature(i) feature_json_trash = feature_trash.ExportToJson() feature_json_trash = eval(feature_json_trash) if 'rest' in feature_json_trash['properties']['FRACTIE1']: #weird reason it's multipoint so all the same point, but more of them stopcoordX = feature_json_trash['geometry'][ 'coordinates'][0][0] stopcoordY = feature_json_trash['geometry'][ 'coordinates'][0][1] #define search radius, only possible if CRS is in meters. if (float(stopcoordX) > startcoordX - searchdist and float(stopcoordX) < startcoordX + searchdist ) and (float(stopcoordY) > startcoordY - searchdist and float(stopcoordY) < startcoordY + searchdist): #if distance is super close then just do this: if distanceR2(startcoordX, startcoordY, stopcoordX, stopcoordY) < closest_trash_dist: print('super close trash found!') stopCoord = (stopcoordX, stopcoordY) #for test underlaying 'costandhotspot' is changed back into the previous array' pathArray, costvalue = createPath( CostSurfacefn, costandhotspotarray, startCoord, stopCoord) Multiline = array2multiline( pathArray, CostSurfacefn, pixelValue) geom = ogr.CreateGeometryFromWkt( str(Multiline)) length = geom.Length() trashinfo = [] trashinfo.append(dict(feature_json_house)) trashinfo.append(float(length)) trashinfo.append(str(Multiline)) trashinfo.append( str(feature_json_trash['properties'] ['LOCATIENR'])) trashinfo.append(float(costvalue)) trashinfo.append( str(feature_json_trash['properties'] ['FRACTIE1'])) considerable_trashcans.append(trashinfo) break else: stopCoord = (stopcoordX, stopcoordY) pathArray, costvalue = createPath( CostSurfacefn, costandhotspotarray, startCoord, stopCoord) Multiline = array2multiline( pathArray, CostSurfacefn, pixelValue) geom = ogr.CreateGeometryFromWkt( str(Multiline)) length = geom.Length() trashinfo = [] trashinfo.append(dict(feature_json_house)) trashinfo.append(float(length)) trashinfo.append(str(Multiline)) trashinfo.append( str(feature_json_trash['properties'] ['LOCATIENR'])) trashinfo.append(float(costvalue)) trashinfo.append( str(feature_json_trash['properties'] ['FRACTIE1'])) considerable_trashcans.append(trashinfo) #find closest in list & check if empty, make BIGGER search (double?) radius and pic the closest (THIS IS SLOW, BUT NEVER NECESARRY) if considerable_trashcans == []: print('==========Nothing in radius') further_trashcans = {} for i in range(shape_trash.GetFeatureCount()): feature_trash = shape_trash.GetFeature(i) feature_json_trash = feature_trash.ExportToJson() feature_json_trash = eval(feature_json_trash) #weird reason it's multipoint so all the same point, but more of them stopcoordX = feature_json_trash['geometry'][ 'coordinates'][0][0] stopcoordY = feature_json_trash['geometry'][ 'coordinates'][0][1] biggersearch = 200 if ( float(stopcoordX) > startcoordX - biggersearch and float(stopcoordX) < startcoordX + biggersearch ) and (float(stopcoordY) > startcoordY - biggersearch and float(stopcoordY) < startcoordY + biggersearch): stopCoord = (stopcoordX, stopcoordY) distance = float( distanceR2(startcoordX, startcoordY, stopcoordX, stopcoordY)) further_trashcans[distance] = ( stopCoord, str(feature_json_trash['properties'] ['LOCATIENR'])) closest_trash = min(further_trashcans) stopCoordNew = further_trashcans[closest_trash][0] LOCATIENR = further_trashcans[closest_trash][1] #for test underlaying 'costandhotspot' is changed back into the previous array' pathArray, costvalue = createPath(CostSurfacefn, costandhotspotarray, startCoord, stopCoordNew) Multiline = array2multiline(pathArray, CostSurfacefn, pixelValue) geom = ogr.CreateGeometryFromWkt(str(Multiline)) length = geom.Length() trashinfo = [] trashinfo.append(dict(feature_json_house)) trashinfo.append(float(length)) trashinfo.append(str(Multiline)) trashinfo.append(str(LOCATIENR)) trashinfo.append( str(feature_json_trash['properties']['FRACTIE1'])) f.write( str(trashinfo[2]) + '\t' + str(trashinfo[0]["properties"]["Identifica"]) + '\t' + str(float(trashinfo[1])) + '\t' + str(trashinfo[3])) papier = [] glas = [] plastic = [] for i in range(shape_trash.GetFeatureCount()): feature_trash = shape_trash.GetFeature(i) feature_json_trash = feature_trash.ExportToJson() feature_json_trash = eval(feature_json_trash) stopcoordX = feature_json_trash['geometry'][ 'coordinates'][0][0] stopcoordY = feature_json_trash['geometry'][ 'coordinates'][0][1] biggersearch = 250 if ( float(stopcoordX) > startcoordX - biggersearch and float(stopcoordX) < startcoordX + biggersearch ) and (float(stopcoordY) > startcoordY - biggersearch and float(stopcoordY) < startcoordY + biggersearch): if 'papier' not in trashinfo[4]: if 'papier' in feature_json_trash[ 'properties']['FRACTIE1']: distance = int( distanceR2(startcoordX, startcoordY, stopcoordX, stopcoordY)) papier.append( (feature_json_trash['properties'] ['LOCATIENR'], distance, (stopcoordX, stopcoordY))) if 'glass' not in trashinfo[4]: if 'glass' in feature_json_trash['properties'][ 'FRACTIE1']: distance = int( distanceR2(startcoordX, startcoordY, stopcoordX, stopcoordY)) glas.append( (feature_json_trash['properties'] ['LOCATIENR'], distance, (stopcoordX, stopcoordY))) if 'kunststof' not in trashinfo[4]: if 'kunststof' in feature_json_trash[ 'properties']['FRACTIE1']: distance = int( distanceR2(startcoordX, startcoordY, stopcoordX, stopcoordY)) plastic.append( (feature_json_trash['properties'] ['LOCATIENR'], distance, (stopcoordX, stopcoordY))) if papier == []: f.write('\t' + '' + '\t' + '') Multiline_papier = '' closest_papier = '', '' else: closest_papier = min(papier, key=lambda t: t[1]) stopCoord = closest_papier[2] pathArray_papier, costvalue = createPath( CostSurfacefn, costSurfaceArray, startCoord, stopCoord) Multiline_papier = array2multiline( pathArray_papier, CostSurfacefn, pixelValue) f.write('\t' + str(Multiline_papier) + '\t' + str(closest_papier[0])) if glas == []: f.write('\t' + '' + '\t' + '') Multiline_glas = '' closest_glas = '', '' else: closest_glas = min(glas, key=lambda t: t[1]) stopCoord = closest_glas[2] pathArray_glas, costvalue = createPath( CostSurfacefn, costSurfaceArray, startCoord, stopCoord) Multiline_glas = array2multiline( pathArray_glas, CostSurfacefn, pixelValue) f.write('\t' + str(Multiline_glas) + '\t' + str(closest_glas[0])) if plastic == []: f.write('\t' + '' + '\t' + '') Multiline_plastic = '' closest_plastic = '', '' else: closest_plastic = min(plastic, key=lambda t: t[1]) stopCoord = closest_plastic[2] pathArray_plastic, costvalue = createPath( CostSurfacefn, costSurfaceArray, startCoord, stopCoord) Multiline_plastic = array2multiline( pathArray_plastic, CostSurfacefn, pixelValue) f.write('\t' + str(Multiline_plastic) + '\t' + str(closest_plastic[0])) f.write('\n') already_made[trashinfo[0]['properties']['id_pand']] = ( float(trashinfo[1]), str(trashinfo[2]), str(trashinfo[0]["properties"]["Identifica"]), int(trashinfo[3]), Multiline_papier, closest_papier[0], Multiline_glas, closest_glas[0], Multiline_plastic, closest_plastic[0]) print('==========exception found') else: closest_trash = min(considerable_trashcans, key=lambda t: t[4]) f.write( str(closest_trash[2]) + '\t' + str(closest_trash[0]["properties"]["Identifica"]) + '\t' + str(float(closest_trash[1])) + '\t' + str(closest_trash[3])) #here we're checking which additional trashcans can be added, based on the fact that we need all 4 of the kinds (glass, paper, plastic) papier = [] glas = [] plastic = [] for i in range(shape_trash.GetFeatureCount()): feature_trash = shape_trash.GetFeature(i) feature_json_trash = feature_trash.ExportToJson() feature_json_trash = eval(feature_json_trash) stopcoordX = feature_json_trash['geometry'][ 'coordinates'][0][0] stopcoordY = feature_json_trash['geometry'][ 'coordinates'][0][1] biggersearch = 250 if ( float(stopcoordX) > startcoordX - biggersearch and float(stopcoordX) < startcoordX + biggersearch ) and (float(stopcoordY) > startcoordY - biggersearch and float(stopcoordY) < startcoordY + biggersearch): if 'papier' not in closest_trash[5]: if 'papier' in feature_json_trash[ 'properties']['FRACTIE1']: distance = int( distanceR2(startcoordX, startcoordY, stopcoordX, stopcoordY)) papier.append( (feature_json_trash['properties'] ['LOCATIENR'], distance, (stopcoordX, stopcoordY))) if 'glas' not in closest_trash[5]: if 'glas' in feature_json_trash['properties'][ 'FRACTIE1']: distance = int( distanceR2(startcoordX, startcoordY, stopcoordX, stopcoordY)) glas.append( (feature_json_trash['properties'] ['LOCATIENR'], distance, (stopcoordX, stopcoordY))) if 'kunststof' not in closest_trash[5]: if 'kunststof' in feature_json_trash[ 'properties']['FRACTIE1']: distance = int( distanceR2(startcoordX, startcoordY, stopcoordX, stopcoordY)) plastic.append( (feature_json_trash['properties'] ['LOCATIENR'], distance, (stopcoordX, stopcoordY))) if papier == []: f.write('\t' + '' + '\t' + '') Multiline_papier = '' closest_papier = '', '' else: closest_papier = min(papier, key=lambda t: t[1]) stopCoord = closest_papier[2] pathArray_papier, costvalue = createPath( CostSurfacefn, costSurfaceArray, startCoord, stopCoord) Multiline_papier = array2multiline( pathArray_papier, CostSurfacefn, pixelValue) f.write('\t' + str(Multiline_papier) + '\t' + str(closest_papier[0])) if glas == []: f.write('\t' + '' + '\t' + '') Multiline_glas = '' closest_glas = '', '' else: closest_glas = min(glas, key=lambda t: t[1]) stopCoord = closest_glas[2] pathArray_glas, costvalue = createPath( CostSurfacefn, costSurfaceArray, startCoord, stopCoord) Multiline_glas = array2multiline( pathArray_glas, CostSurfacefn, pixelValue) f.write('\t' + str(Multiline_glas) + '\t' + str(closest_glas[0])) if plastic == []: f.write('\t' + '' + '\t' + '') Multiline_plastic = '' closest_plastic = '', '' else: closest_plastic = min(plastic, key=lambda t: t[1]) stopCoord = closest_plastic[2] pathArray_plastic, costvalue = createPath( CostSurfacefn, costSurfaceArray, startCoord, stopCoord) Multiline_plastic = array2multiline( pathArray_plastic, CostSurfacefn, pixelValue) f.write('\t' + str(Multiline_plastic) + '\t' + str(closest_plastic[0])) f.write('\n') already_made[closest_trash[0]['properties']['id_pand']] = ( float(closest_trash[1]), str(closest_trash[2]), str(closest_trash[0]["properties"]["Identifica"]), int(closest_trash[3]), Multiline_papier, closest_papier[0], Multiline_glas, closest_glas[0], Multiline_plastic, closest_plastic[0]) print('==========best found=========') #for now not important yet array2raster(CostSurfacefn, newRasterfn, costandhotspotarray)
def identifyIntersections(in_shp_pth, out_shp_pth, id_field="ID"): """ VERY SLOW Identifies intersections between polygons of a shapefile. Writes intersections specfied output path. :param in_shp_pth: Input shapefile of polygons. :param out_pth: Output path to which the intersections are written. Input filename will be extended by "_intersections". :return: No object returned, but shapefile will be written to disc. """ import os import ogr import vector in_shp = ogr.Open(in_shp_pth, 0) in_lyr = in_shp.GetLayer() fname_lst = vector.getFieldNames(in_shp) copy_shp, copy_lyr = vector.copyLayerToMemory(in_lyr) drv_shp = ogr.GetDriverByName('ESRI Shapefile') in_sr = in_lyr.GetSpatialRef() in_lyr_defn = in_lyr.GetLayerDefn() if os.path.exists(out_shp_pth): drv_shp.DeleteDataSource(out_shp_pth) inters_shp = drv_shp.CreateDataSource(out_shp_pth) lyr_name = os.path.splitext(os.path.split(out_shp_pth)[1])[0] geom_type = ogr.wkbPolygon inters_lyr = inters_shp.CreateLayer(lyr_name, in_sr, geom_type=geom_type) for i in range(0, in_lyr_defn.GetFieldCount()): field_def = in_lyr_defn.GetFieldDefn(i) inters_lyr.CreateField(field_def) # inters_lyr.CreateField(ogr.FieldDefn('ID', ogr.OFTInteger64)) inters_lyr.CreateField(ogr.FieldDefn('IDInters', ogr.OFTString)) inters_lyr_defn = inters_lyr.GetLayerDefn() num_fields = inters_lyr_defn.GetFieldCount() id_inters_lst = [] for feat_curr in in_lyr: id1 = feat_curr.GetField(id_field) # print("FEATURE: {}".format(id1)) geom_curr = feat_curr.GetGeometryRef() copy_lyr.SetSpatialFilter(geom_curr) for feat_nb in copy_lyr: id2 = feat_nb.GetField(id_field) id_inters = '{0}_{1}'.format(min([id1, id2]), max([id1, id2])) geom_nb = feat_nb.geometry() if id1 != id2: # print("Neighbouring features: {}".format(id2)) if geom_nb.Intersects(geom_curr): intersection = geom_nb.Intersection(geom_curr) if intersection == None: area_inters = 0 else: geom_type = intersection.GetGeometryName() if geom_type not in [ 'POLYGON', 'MULTIPOLYGON' ]: # in ['MULTILINESTRING', 'POINT', 'LINESTRING','MULTIPOINT']: #alternatively intersection = None area_inters = 0 else: area_inters = round(intersection.Area(), 1) else: intersection = None area_inters = 0 ## if the id of the intersection is not already in the list and its area is bigger than 0 ## then add this feature to the intersection layer if area_inters > 0.0 and id_inters not in id_inters_lst: intersection = intersection.Buffer(0) intersection = intersection.MakeValid() wkt_inters = intersection.ExportToWkt() poly = ogr.CreateGeometryFromWkt(wkt_inters) out_feat = ogr.Feature(inters_lyr_defn) out_feat.SetGeometry(poly) for fname in fname_lst: ind = fname_lst.index(fname) attr = feat_curr.GetField(fname) out_feat.SetField(ind, attr) ind = len(fname_lst) out_feat.SetField(ind, id_inters) inters_lyr.CreateFeature(out_feat) ouf_feat = None id_inters_lst.append(id_inters) else: pass copy_lyr.SetSpatialFilter(None) copy_lyr.ResetReading() in_lyr.ResetReading() inters_lyr.ResetReading() del copy_shp, copy_lyr del inters_shp, inters_lyr del in_shp, in_lyr
def add_wkt(self, wkt, value): geometry = ogr.CreateGeometryFromWkt(wkt) self.add_geometry(geometry, value)
def point2Shp(df_osm, valueArray, fn, pt_lyrName_w, ref_lyr=False): ds = ogr.Open(fn, 1) # '''参考层,用于空间坐标投影,字段属性等参照''' # ref_lyr=ds.GetLayer(ref_lyr) # ref_sr=ref_lyr.GetSpatialRef() # print(ref_sr) # ref_schema=ref_lyr.schema #查看属性表字段名和类型 # for field in ref_schema: # print(field.name,field.GetTypeName()) '''建立新的datasource数据源''' sf_driver = ogr.GetDriverByName('ESRI Shapefile') sfDS = os.path.join(fn, r'sf') # if os.path.exists(sfDS): # sf_driver.DeleteDataSource(sfDS) pt_ds = sf_driver.CreateDataSource(sfDS) if pt_ds is None: sys.exit('Could not open{0}'.format(sfDS)) '''建立新layer层''' if pt_ds.GetLayer(pt_lyrName_w): pt_ds.DeleteLayer(pt_lyrName_w) spatialRef = osr.SpatialReference() spatialRef.SetWellKnownGeogCS( "WGS84") #需要注意直接定义大地坐标未"WGS84",而未使用参考层提取的坐标投影系统 pt_lyr = pt_ds.CreateLayer(pt_lyrName_w, spatialRef, ogr.wkbPoint) # pt_lyr=pt_ds.CreateLayer(pt_lyrName_w,ref_sr,ogr.wkbPoint) '''配置字段,名称以及类型和相关参数''' # pt_lyr.CreateFields(ref_schema) LatFd = ogr.FieldDefn("origiLat", ogr.OFTReal) LatFd.SetWidth(20) LatFd.SetPrecision(3) pt_lyr.CreateField(LatFd) LatFd.SetName("origiLong") pt_lyr.CreateField(LatFd) # pt_lyr.CreateFields(ref_schema) preFd = ogr.FieldDefn("type", ogr.OFTString) pt_lyr.CreateField(preFd) preFd.SetName("tagkey") pt_lyr.CreateField(preFd) preFd.SetName("tagvalue") pt_lyr.CreateField(preFd) preFd = ogr.FieldDefn("cluster", ogr.OFTInteger) pt_lyr.CreateField(preFd) # preFd.SetName("cluster") # pt_lyr.CreateField(preFd) # stationName=ogr.FieldDefn("stationN",ogr.OFTString) # pt_lyr.CreateField(stationName) # preFd.SetName("ObservTime") # pt_lyr.CreateField(preFd) # '''建立feature空特征和设置geometry几何类型''' print(pt_lyr.GetLayerDefn()) pt_feat = ogr.Feature(pt_lyr.GetLayerDefn()) # idx=0 for i in tqdm(range(valueArray.shape[0])): #循环feature # print(key) '''设置几何体''' #pt_ref=feat.geometry().Clone() # converCoordiGCJ=cc.bd09togcj02(dataBunch.data[i][1],dataBunch.data[i][0]) # converCoordiGPS84=cc.gcj02towgs84(converCoordiGCJ[0],converCoordiGCJ[1]) # print(wdCoordiDicSingle[key][1],wdCoordiDicSingle[key][0]) # print(converCoordiGPS84[0], converCoordiGPS84[1]) wkt = "POINT(%f %f)" % (df_osm["lon"][i], df_osm["lat"][i]) # wkt="POINT(%f %f)" % (dataBunch.data[i][0], dataBunch.data[i][1]) newPt = ogr.CreateGeometryFromWkt(wkt) #使用wkt的方法建立点 pt_feat.SetGeometry(newPt) '''设置字段值''' # for i_field in range(feat.GetFieldCount()): # pt_feat.SetField(i_field,feat.GetField(i_field)) pt_feat.SetField("origiLat", df_osm["lat"][i]) pt_feat.SetField("origiLong", df_osm["lon"][i]) # print(wdDicComplete[key]['20140901190000']) pt_feat.SetField("type", df_osm["type"][i]) # pt_feat.SetField("tagkey", df_osm["tagkey"][i]) pt_feat.SetField("tagvalue", df_osm["tagvalue"][i]) pt_feat.SetField("cluster", int(valueArray[i])) # print(idx,int(valueArray[idx]),pt_ref.GetX()) # idx+=1 '''根据设置的几何体和字段值,建立feature。循环建立多个feature特征''' pt_lyr.CreateFeature(pt_feat) del ds
burn_doy = burn_ds.variables['burn_date'] burn_month = burn_ds.variables['month'] burn_east = burn_ds.variables['x'][:] burn_north = burn_ds.variables['y'][:] for i, row in group.iterrows(): print(count) count += 1 month = row['month'] year = row['year'] # Reproject coordinates wkt = 'POINT ({lon} {lat})'.format(lon=row['longitude'], lat=row['latitude']) location = ogr.CreateGeometryFromWkt(wkt) location.Transform(latlon_to_utm) slide_x = location.GetX() slide_y = location.GetY() if row['location_accuracy'] in ('exact', 'unknown'): event_burn = burn_ds.sel(x=slide_x, y=slide_y, method='nearest') total = 1. else: level = int(row['location_accuracy'][:-2]) radius = level * 1000 # Pull data event_burn = burn_ds.where(
def wkbwkt_geom( self ): raw_wkb = open('data/wkb_wkt/' + self.unit + '.wkb','rb').read() raw_wkt = open('data/wkb_wkt/' + self.unit + '.wkt').read() ###################################################################### # Compare the WKT derived from the WKB file to the WKT provided # but reformatted (normalized). geom_wkb = ogr.CreateGeometryFromWkb( raw_wkb ) wkb_wkt = geom_wkb.ExportToWkt() geom_wkt = ogr.CreateGeometryFromWkt( raw_wkt ) normal_wkt = geom_wkt.ExportToWkt() if wkb_wkt != normal_wkt: gdaltest.post_reason( 'WKT from WKB (%s) does not match clean WKT (%s).' % (wkb_wkt, normal_wkt) ) return 'fail' ###################################################################### # Verify that the geometries appear to be the same. This is # intended to catch problems with the encoding too WKT that might # cause passes above but that are mistaken. if geom_wkb.GetCoordinateDimension() != geom_wkt.GetCoordinateDimension(): gdaltest.post_reason( 'Coordinate dimension differs!' ) return 'fail' if geom_wkb.GetGeometryType() != geom_wkt.GetGeometryType(): gdaltest.post_reason( 'Geometry type differs!' ) return 'fail' if geom_wkb.GetGeometryName() != geom_wkt.GetGeometryName(): gdaltest.post_reason( 'Geometry name differs!' ) return 'fail' # It turns out this test is too picky about coordinate precision. skip. # if geom_wkb.Equal( geom_wkt ) == 0: # gdaltest.post_reason( 'Geometries not equal!' ) # print geom_wkb.ExportToWkt() # print geom_wkt.ExportToWkt() # return 'fail' geom_wkb.Destroy() ###################################################################### # Convert geometry to WKB and back to verify that WKB encoding is # working smoothly. wkb_xdr = geom_wkt.ExportToWkb( ogr.wkbXDR ) geom_wkb = ogr.CreateGeometryFromWkb( wkb_xdr ) if str(geom_wkb) != str(geom_wkt): print(geom_wkb) print(geom_wkt) gdaltest.post_reason( 'XDR WKB encoding/decoding failure.' ) return 'fail' geom_wkb.Destroy() wkb_ndr = geom_wkt.ExportToWkb( ogr.wkbNDR ) geom_wkb = ogr.CreateGeometryFromWkb( wkb_ndr ) if str(geom_wkb) != str(geom_wkt): gdaltest.post_reason( 'NDR WKB encoding/decoding failure.' ) return 'fail' geom_wkb.Destroy() geom_wkt.Destroy() return 'success'
def ogr_vrt_14(): if gdaltest.vrt_ds is None: return 'skip' gdal.PushErrorHandler('CPLQuietErrorHandler') try: ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test.shp') except: pass gdal.PopErrorHandler() shp_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource( 'tmp/test.shp') shp_lyr = shp_ds.CreateLayer('test') feat = ogr.Feature(shp_lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (-10 49)') feat.SetGeometryDirectly(geom) shp_lyr.CreateFeature(feat) feat.Destroy() feat = ogr.Feature(shp_lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (-10 49)') feat.SetGeometryDirectly(geom) shp_lyr.CreateFeature(feat) feat.Destroy() feat = ogr.Feature(shp_lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (2 49)') feat.SetGeometryDirectly(geom) shp_lyr.CreateFeature(feat) feat.Destroy() feat = ogr.Feature(shp_lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (-10 49)') feat.SetGeometryDirectly(geom) shp_lyr.CreateFeature(feat) feat.Destroy() shp_ds.ExecuteSQL('CREATE SPATIAL INDEX on test') shp_ds.Destroy() vrt_xml = """ <OGRVRTDataSource> <OGRVRTLayer name="test"> <SrcDataSource relativeToVRT="0">tmp/test.shp</SrcDataSource> <SrcLayer>test</SrcLayer> <SrcRegion>POLYGON((0 40,0 50,10 50,10 40,0 40))</SrcRegion> </OGRVRTLayer> </OGRVRTDataSource>""" vrt_ds = ogr.Open(vrt_xml) vrt_lyr = vrt_ds.GetLayerByName('test') if vrt_lyr.TestCapability(ogr.OLCFastSpatialFilter) != 1: gdaltest.post_reason('Fast filter not set.') return 'fail' extent = vrt_lyr.GetExtent() if extent != (2.0, 2.0, 49.0, 49.0): gdaltest.post_reason('wrong extent') print(extent) return 'fail' if vrt_lyr.GetFeatureCount() != 1: gdaltest.post_reason('Feature count not one as expected.') return 'fail' feat = vrt_lyr.GetNextFeature() if feat.GetFID() != 2: gdaltest.post_reason('did not get fid 2.') return 'fail' geom = feat.GetGeometryRef() if geom.ExportToWkt() != 'POINT (2 49)': gdaltest.post_reason('did not get expected point geometry.') return 'fail' feat.Destroy() vrt_lyr.SetSpatialFilterRect(1, 41, 3, 49.5) if vrt_lyr.GetFeatureCount() != 1: if gdal.GetLastErrorMsg().find('GEOS support not enabled') != -1: ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource( 'tmp/test.shp') return 'skip' print(vrt_lyr.GetFeatureCount()) gdaltest.post_reason('did not get one feature on rect spatial filter.') return 'fail' vrt_lyr.SetSpatialFilterRect(1, 41, 3, 48.5) if vrt_lyr.GetFeatureCount() != 0: gdaltest.post_reason('Did not get expected zero feature count.') return 'fail' vrt_lyr.SetSpatialFilter(None) if vrt_lyr.GetFeatureCount() != 1: gdaltest.post_reason( 'Did not get expected one feature count with no filter.') return 'fail' vrt_ds.Destroy() vrt_ds = None ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test.shp') return 'success'
def write_shp(self, label): """ --convert the pixel location to latitude and longitude --write all objects in a certain image into a single shp file """ shapefilename = os.path.join( self.outputFolder_Path, get_filename(self.inputFile_Path, is_suffix=False) + '_%s_shp.shp' % self.class_names[label]) class_bndbox = self.run_info['bndboxes'] # 获取图像六元数 0-经度 1-经度分辨率 3-纬度 5-纬度分辨率 lat_lon_init = get_geoTransform(self.inputFile_Path) # 注册所有的驱动 ogr.RegisterAll() # 创建数据 strDriverName = 'ESRI Shapefile' oDriver = ogr.GetDriverByName(strDriverName) if oDriver == None: print("%s 驱动不可用!\n", strDriverName) # 创建数据源 oDS = oDriver.CreateDataSource(shapefilename) if oDS == None: print("创建文件【%s】失败!", shapefilename) # 创建图层 outLayer = oDS.CreateLayer('detection', geom_type=ogr.wkbPolygon) # papszLCO = [] # outLayer = oDS.CreateLayer("TestPolygon", None, ogr.wkbPolygon, papszLCO) # oDefn = outLayer.GetLayerDefn() # # 创建矩形要素 # oFeatureRectangle = ogr.Feature(oDefn) # oFeatureRectangle.SetField(0, 1) # oFeatureRectangle.SetField(1, "rect1") # geomRectangle = ogr.CreateGeometryFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))") # oFeatureRectangle.SetGeometry(geomRectangle) # outLayer.CreateFeature(oFeatureRectangle) # create fields fieldDefn2 = ogr.FieldDefn('class', ogr.OFTInteger) fieldDefn2.SetWidth(10) outLayer.CreateField(fieldDefn2, 1) # get feature defintion outFeatureDefn = outLayer.GetLayerDefn() for object in class_bndbox: # wkt = "POINT(%f %f)" % (float(pointListX[i]), float(pointListY[i])) # point = ogr.CreateGeometryFromWkt(wkt) # outFeature.SetGeometry(point) if self.class_names.index(object[0]) != label: continue # 坐标转换 Ymin = object[1] * lat_lon_init[5] + lat_lon_init[3] Xmin = object[2] * lat_lon_init[1] + lat_lon_init[0] Ymax = object[3] * lat_lon_init[5] + lat_lon_init[3] Xmax = object[4] * lat_lon_init[1] + lat_lon_init[0] oFeatureRectancle = ogr.Feature(outFeatureDefn) oFeatureRectancle.SetField(0, self.class_names.index(object[0]) + 1) polygon_cmd = 'POLYGON ((%f %f,%f %f,%f %f,%f %f,%f %f))' % ( Xmin, Ymin, Xmin, Ymax, Xmax, Ymax, Xmax, Ymin, Xmin, Ymin) geomRectancle = ogr.CreateGeometryFromWkt(polygon_cmd) oFeatureRectancle.SetGeometry(geomRectancle) outLayer.CreateFeature(oFeatureRectancle) oFeatureRectancle.Destroy() oDS.Destroy() print('shp finished!') pass
def ogr_vrt_20(): if gdaltest.vrt_ds is None: return 'skip' gdal.PushErrorHandler('CPLQuietErrorHandler') try: ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test.shp') except: pass gdal.PopErrorHandler() shp_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource( 'tmp/test.shp') shp_lyr = shp_ds.CreateLayer('test') feat = ogr.Feature(shp_lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (-10 45)') feat.SetGeometryDirectly(geom) shp_lyr.CreateFeature(feat) feat.Destroy() feat = ogr.Feature(shp_lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (-10 49)') feat.SetGeometryDirectly(geom) shp_lyr.CreateFeature(feat) feat.Destroy() feat = ogr.Feature(shp_lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (2 49)') feat.SetGeometryDirectly(geom) shp_lyr.CreateFeature(feat) feat.Destroy() feat = ogr.Feature(shp_lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (-10 49)') feat.SetGeometryDirectly(geom) shp_lyr.CreateFeature(feat) feat.Destroy() shp_ds.ExecuteSQL('CREATE SPATIAL INDEX on test') shp_ds.Destroy() vrt_xml = """ <OGRVRTDataSource> <OGRVRTLayer name="test"> <SrcDataSource relativeToVRT="0">tmp/test.shp</SrcDataSource> <SrcLayer>test</SrcLayer> </OGRVRTLayer> </OGRVRTDataSource>""" vrt_ds = ogr.Open(vrt_xml) vrt_lyr = vrt_ds.GetLayerByName('test') if vrt_lyr.TestCapability(ogr.OLCFastFeatureCount) != 1: gdaltest.post_reason('Fast feature count not set.') return 'fail' if vrt_lyr.TestCapability(ogr.OLCFastSpatialFilter) != 1: gdaltest.post_reason('Fast filter not set.') return 'fail' if vrt_lyr.TestCapability(ogr.OLCFastGetExtent) != 1: gdaltest.post_reason('Fast extent not set.') return 'fail' extent = vrt_lyr.GetExtent() if extent != (-10.0, 2.0, 45.0, 49.0): gdaltest.post_reason('wrong extent') print(extent) return 'fail' if vrt_lyr.GetFeatureCount() != 4: gdaltest.post_reason('Feature count not 4 as expected.') return 'fail' vrt_lyr.SetSpatialFilterRect(1, 48.5, 3, 49.5) if vrt_lyr.GetFeatureCount() != 1: if gdal.GetLastErrorMsg().find('GEOS support not enabled') != -1: ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource( 'tmp/test.shp') return 'skip' print(vrt_lyr.GetFeatureCount()) gdaltest.post_reason('did not get one feature on rect spatial filter.') return 'fail' if vrt_lyr.TestCapability(ogr.OLCFastFeatureCount) != 1: gdaltest.post_reason('Fast feature count not set.') return 'fail' if vrt_lyr.TestCapability(ogr.OLCFastGetExtent) != 1: gdaltest.post_reason('Fast extent not set.') return 'fail' extent = vrt_lyr.GetExtent() # the shapefile driver currently doesn't change the extent even in the # presence of a spatial filter, so that could change in the future if extent != (-10.0, 2.0, 45.0, 49.0): gdaltest.post_reason('wrong extent') print(extent) return 'fail' vrt_lyr.SetSpatialFilterRect(1, 48, 3, 48.5) if vrt_lyr.GetFeatureCount() != 0: gdaltest.post_reason('Did not get expected zero feature count.') return 'fail' vrt_lyr.SetSpatialFilter(None) if vrt_lyr.GetFeatureCount() != 4: gdaltest.post_reason('Feature count not 4 as expected with no filter.') return 'fail' vrt_ds.Destroy() vrt_ds = None ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test.shp') return 'success'
def add_los_data(basedir: Path, data_source: ogr.DataSource, ts_source: sqlite3.Connection): """Add ascending/descending data and matching timeseries""" point_lyr = data_source.GetLayerByName("LOS") if ts_source: cur = ts_source.cursor() cur.execute("PRAGMA synchronous = OFF") cur.execute("BEGIN TRANSACTION") key = 0 for raw_shp, cal_shp in zip(basedir["raw"], basedir["cal"]): with fiona.open(str(raw_shp), "r") as raw, fiona.open(str(cal_shp), "r") as cal: track_no = re.match("LOS_(\d*)[A|D].*", raw_shp.stem)[1] match = re.match("LOS_\d{1,3}(\D)(_\d_)?", raw_shp.stem) track_dir = re.match("LOS_\d{1,3}(\D)(_\d_)?", raw_shp.stem)[1] time_series_dates = np.array(parse_dates(raw.schema)) data_source.StartTransaction() with click.progressbar(raw, label="Track: {}, Dir: {}".format( track_no, track_dir)) as raw_progress: # assume that raw and calibrated files are of equal lengths for fr, fc in zip(raw_progress, cal): point = ogr.CreateGeometryFromWkt( "POINT({} {})".format(*fr["geometry"]["coordinates"])) feature = ogr.Feature(point_lyr.GetLayerDefn()) feature.SetField("CODE", fr["properties"]["CODE"]) feature.SetField("HEIGHT", fr["properties"]["HEIGHT"]) feature.SetField("H_STDEV", fr["properties"]["H_STDEV"]) feature.SetField("TRACK_NO", track_no) feature.SetField("DIR", track_dir) feature.SetField("VEL", fr["properties"]["VEL"]) feature.SetField("V_STDEV", fr["properties"]["V_STDEV"]) feature.SetField("COHERENCE", fr["properties"]["COHERENCE"]) feature.SetField("ER_BAR", fr["properties"]["ER_BAR"]) feature.SetField("EFF_AREA", fr["properties"]["EFF_AREA"]) feature.SetField("LOS_H", fr["properties"]["LOS_H"]) feature.SetField("VEL_VERT", fr["properties"]["VEL_VERT"]) feature.SetField("VEL_CAL", fc["properties"]["VEL"]) feature.SetField("VEL_STDEV_CAL", fc["properties"]["V_STDEV"]) # Add statistics raw_y = np.array([ v for k, v in fr["properties"].items() if k.startswith("D") ]) cal_y = np.array([ v for k, v in fc["properties"].items() if k.startswith("D") ]) if ts_source: code = fr["properties"]["CODE"] # Geopackage layers has internal primary key 'fid' that autoincrements # and is therefore the same as this key variable. Saving a bit of space # by not repeating it. sql_code = f"INSERT INTO ts_los_codes(code) VALUES ('{code}')" cur.execute(sql_code) key = key + 1 sql_ts = f"INSERT INTO ts_los_t(code_key, time, value, cal_value) VALUES ('{key}', ?, ?, ?)" cur.executemany(sql_ts, zip(time_series_dates, raw_y, cal_y)) ts_source.commit() (periodicity, periodicity_stddev) = calc_periodicity( time_series_dates, raw_y) feature.SetField("PERIOD", periodicity) feature.SetField("PERIOD_STDEV", periodicity_stddev) feature.SetGeometry(point) feature.SetFID(point_lyr.GetFeatureCount()) point_lyr.CreateFeature(feature) feature.Destroy data_source.CommitTransaction() data_source.SyncToDisk()
def ogr_gft_write(): if ogrtest.gft_drv is None: return 'skip' if ogrtest.gft_refresh is None: ogrtest.gft_can_write = False return 'skip' ds = ogr.Open('GFT:refresh=%s' % ogrtest.gft_refresh, update = 1) if ds is None: ogrtest.gft_can_write = False return 'skip' ogrtest.gft_can_write = True import random ogrtest.gft_rand_val = random.randint(0,2147000000) table_name = "test_%d" % ogrtest.gft_rand_val lyr = ds.CreateLayer(table_name) lyr.CreateField(ogr.FieldDefn('strcol', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn('numcol', ogr.OFTReal)) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('strcol', 'foo') feat.SetField('numcol', '3.45') expected_wkt = "POLYGON ((0 0,0 1,1 1,1 0),(0.25 0.25,0.25 0.75,0.75 0.75,0.75 0.25))" geom = ogr.CreateGeometryFromWkt(expected_wkt) feat.SetGeometry(geom) if lyr.CreateFeature(feat) != 0: gdaltest.post_reason('CreateFeature() failed') return 'fail' fid = feat.GetFID() feat.SetField('strcol', 'bar') if lyr.SetFeature(feat) != 0: gdaltest.post_reason('SetFeature() failed') return 'fail' lyr.ResetReading() feat = lyr.GetNextFeature() if feat.GetFieldAsString('strcol') != 'bar': gdaltest.post_reason('GetNextFeature() did not get expected feature') feat.DumpReadable() return 'fail' feat = lyr.GetFeature(fid) if feat.GetFieldAsString('strcol') != 'bar': gdaltest.post_reason('GetFeature() did not get expected feature') feat.DumpReadable() return 'fail' got_wkt = feat.GetGeometryRef().ExportToWkt() if got_wkt != expected_wkt: gdaltest.post_reason('did not get expected geometry') print(got_wkt) return 'fail' if lyr.GetFeatureCount() != 1: gdaltest.post_reason('GetFeatureCount() did not returned expected value') return 'fail' if lyr.DeleteFeature(feat.GetFID()) != 0: gdaltest.post_reason('DeleteFeature() failed') return 'fail' ds.ExecuteSQL('DELLAYER:%s' % table_name) ds = None return 'success'
def vector_to_envelope_geom(extent_path, epsg=28992): geom = vector_to_geom(extent_path, epsg=epsg) x1, x2, y1, y2 = geom.GetEnvelope() wkt = POLYGON.format(x1=x1, x2=x2, y1=y1, y2=y2) geom = ogr.CreateGeometryFromWkt(wkt) return geom
logger.info("Applying attribute filter") imginfo_list2 = filterMatchingImages(imginfo_list, params) if len(imginfo_list2) == 0: logger.error("No valid images found. Check input filter parameters.") sys.exit() else: logger.info("%i images match filter" % len(imginfo_list2)) #### if extent is specified, build tile params and compare extent to input image geom if args.extent: poly_wkt = 'POLYGON (( %f %f, %f %f, %f %f, %f %f, %f %f ))' % ( params.xmin, params.ymin, params.xmin, params.ymax, params.xmax, params.ymax, params.xmax, params.ymin, params.xmin, params.ymin) params.extent_geom = ogr.CreateGeometryFromWkt(poly_wkt) #### Check geom overlaps extent imginfo_list3 = [] for iinfo in imginfo_list2: if iinfo.geom is not None: if params.extent_geom.Intersect(iinfo.geom) is True: imginfo_list3.append(iinfo) else: logger.debug("Image does not intersect mosaic extent: %s" % iinfo.srcfn) else: # remove from list if no geom logger.debug("Null geometry for image: %s" % iinfo.srcfn) #### else set extent after image geoms computed else:
def ogr_wfs_geoserver_wfst(): if gdaltest.wfs_drv is None: return 'skip' if not gdaltest.have_gml_reader: return 'skip' if gdaltest.geoserver_wfs != True: return 'skip' ds = ogr.Open('WFS:http://demo.opengeo.org/geoserver/wfs', update=1) if ds is None: return 'fail' lyr = ds.GetLayerByName('za:za_points') geom = ogr.CreateGeometryFromWkt('POINT(0 89.5)') feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetGeometry(geom) #feat.SetField('name', 'name_set_by_ogr_wfs_8_test') feat.SetField('type', 'type_set_by_ogr_wfs_8_test') if lyr.CreateFeature(feat) != 0: gdaltest.post_reason('cannot create feature') return 'fail' print('Feature %d created !' % feat.GetFID()) feat.SetField('type', 'type_modified_by_ogr_wfs_8_test') if lyr.SetFeature(feat) != 0: gdaltest.post_reason('cannot update feature') return 'fail' print('Feature %d updated !' % feat.GetFID()) if lyr.DeleteFeature(feat.GetFID()) != 0: gdaltest.post_reason('could not delete feature') return 'fail' print('Feature %d deleted !' % feat.GetFID()) # Test transactions if lyr.StartTransaction() != 0: gdaltest.post_reason('CommitTransaction() failed') return 'fail' geom = ogr.CreateGeometryFromWkt('POINT(0 89.5)') feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetGeometry(geom) #feat.SetField('name', 'name_set_by_ogr_wfs_8_test') feat.SetField('type', 'type_set_by_ogr_wfs_8_test') if lyr.CreateFeature(feat) != 0: gdaltest.post_reason('cannot create feature') return 'fail' geom = ogr.CreateGeometryFromWkt('POINT(0 89.5)') feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetGeometry(geom) #feat.SetField('name', 'name_set_by_ogr_wfs_8_test_2') feat.SetField('type', 'type_set_by_ogr_wfs_8_test_2') if lyr.CreateFeature(feat) != 0: gdaltest.post_reason('cannot create feature') return 'fail' if lyr.CommitTransaction() != 0: gdaltest.post_reason('CommitTransaction() failed') return 'fail' # Retrieve inserted features print('Retrieving created features gml:id') sql_lyr = ds.ExecuteSQL("SELECT _LAST_INSERTED_FIDS_ FROM za:za_points") feat = sql_lyr.GetNextFeature() while feat is not None: gml_id = feat.GetFieldAsString(0) print('Feature %s has been created in transaction !' % gml_id) feat = sql_lyr.GetNextFeature() feat = None count = sql_lyr.GetFeatureCount() ds.ReleaseResultSet(sql_lyr) if count != 2: gdaltest.post_reason('did not get expected feature count') return 'fail' # Delete a bunch of features print('Deleting created features') sql_lyr = ds.ExecuteSQL( "DELETE FROM za:za_points WHERE type = 'type_set_by_ogr_wfs_8_test' OR type = 'type_set_by_ogr_wfs_8_test_2'" ) ds.ReleaseResultSet(sql_lyr) return 'success'
def any_geom2ogr_geom(geom, sref=None): """ Transforms: - bounding box extents [(x_min, y_min), (x_max, y_max)] - bounding box points [x_min, y_min, x_max, y_max] - a list of points [(x_1, y_1), (x_2, y_2), (x_3, y_3), ...] - point coordinates (x_1, y_1) - a `shapely.geometry.Point` instance - a `shapely.geometry.Polygon` instance - a `ogr.Geometry` instance into an OGR geometry object. If the given geometry representation does not contain information about its spatial reference, this information needs to be supplied via `sref`. Parameters ---------- geom : ogr.Geometry or shapely.geometry or list or tuple A vector geometry. It can be a - bounding box extent [(x_min, y_min), (x_max, y_max)] - bounding box point list [x_min, y_min, x_max, y_max] - list of points [(x_1, y_1), (x_2, y_2), (x_3, y_3), ...] - point (x_1, y_1) - `shapely.geometry.Point` instance - `shapely.geometry.Polygon` instance - `ogr.Geometry` instance sref : geospade.crs.SpatialRef, optional Spatial reference system applied to the given geometry if it has none. Returns ------- ogr_geom : ogr.Geometry Vector geometry as an OGR Geometry object including its spatial reference. """ # a list of two 2-tuples containing the bbox coordinates if isinstance(geom, (tuple, list)) and (len(geom) == 2) and isinstance(geom[0], (tuple, list)) \ and isinstance(geom[1], (tuple, list)): ogr_geom = bbox_to_polygon(geom, sref=sref) # a list containing 4 coordinates defining the bbox/extent of the geometry elif isinstance(geom, (tuple, list)) and (len(geom) == 4) and (all([isinstance(x, (float, int)) for x in geom])): bbox_geom = [(geom[0], geom[1]), (geom[2], geom[3])] ogr_geom = any_geom2ogr_geom(bbox_geom, sref=sref) # a list/tuple with point coordinates elif isinstance(geom, (tuple, list)) and (len(geom) == 2) and (all([isinstance(x, (float, int)) for x in geom])): point = shapely.geometry.Point(geom[0], geom[1]) ogr_geom = any_geom2ogr_geom(point) # a list containing many 2-tuples elif isinstance(geom, (tuple, list)) and (all([isinstance(x, (tuple, list)) and (len(x) == 2) for x in geom])): polygon = shapely.geometry.Polygon(geom) ogr_geom = any_geom2ogr_geom(polygon) # a Shapely point or polygon elif isinstance(geom, (shapely.geometry.Polygon, shapely.geometry.Point)): ogr_geom = ogr.CreateGeometryFromWkt(geom.wkt) ogr_geom = any_geom2ogr_geom(ogr_geom, sref=sref) # a OGR geometry elif isinstance(geom, ogr.Geometry): ogr_geom = geom if ogr_geom.GetSpatialReference() is None: if sref is None: raise SrefUnknown() else: ogr_geom.AssignSpatialReference(sref.osr_sref) else: raise GeometryUnknown(geom) return ogr_geom
def ogr_libkml_write(filename): if ogrtest.kml_drv is None: return 'skip' srs = osr.SpatialReference() srs.SetWellKnownGeogCS('WGS72') ds = ogr.GetDriverByName('LIBKML').CreateDataSource(filename) lyr = ds.CreateLayer('test_wgs72', srs=srs) dst_feat = ogr.Feature(lyr.GetLayerDefn()) dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT (2 49)')) if lyr.CreateFeature(dst_feat) != 0: gdaltest.post_reason('CreateFeature failed.') return 'fail' if dst_feat.GetGeometryRef().ExportToWkt() != 'POINT (2 49)': print(dst_feat.GetGeometryRef().ExportToWkt()) gdaltest.post_reason('CreateFeature changed the geometry.') return 'fail' dst_feat.Destroy() lyr = ds.CreateLayer('test_wgs84') fieldefn = ogr.FieldDefn('name', ogr.OFTString) lyr.CreateField(fieldefn) fieldefn = ogr.FieldDefn('description', ogr.OFTString) lyr.CreateField(fieldefn) fieldefn = ogr.FieldDefn('foo', ogr.OFTString) lyr.CreateField(fieldefn) dst_feat = ogr.Feature(lyr.GetLayerDefn()) dst_feat.SetField('name', 'my_name') dst_feat.SetField('description', 'my_description') dst_feat.SetField('foo', 'bar') dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT (2 49)')) if lyr.CreateFeature(dst_feat) != 0: gdaltest.post_reason('CreateFeature failed.') return 'fail' dst_feat.Destroy() dst_feat = ogr.Feature(lyr.GetLayerDefn()) dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT (2 49 1)')) if lyr.CreateFeature(dst_feat) != 0: gdaltest.post_reason('CreateFeature failed.') return 'fail' dst_feat.Destroy() dst_feat = ogr.Feature(lyr.GetLayerDefn()) dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('LINESTRING (0 1,2 3)')) if lyr.CreateFeature(dst_feat) != 0: gdaltest.post_reason('CreateFeature failed.') return 'fail' dst_feat.Destroy() dst_feat = ogr.Feature(lyr.GetLayerDefn()) dst_feat.SetGeometry( ogr.CreateGeometryFromWkt( 'POLYGON ((0 1 0,2 3 0,4 5 0,0 1 0),(0 1 0,2 3 0,4 5 0,0 1 0))')) if lyr.CreateFeature(dst_feat) != 0: gdaltest.post_reason('CreateFeature failed.') return 'fail' dst_feat.Destroy() dst_feat = ogr.Feature(lyr.GetLayerDefn()) dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('MULTIPOINT (2 49,2 49)')) if lyr.CreateFeature(dst_feat) != 0: gdaltest.post_reason('CreateFeature failed.') return 'fail' dst_feat.Destroy() dst_feat = ogr.Feature(lyr.GetLayerDefn()) dst_feat.SetGeometry( ogr.CreateGeometryFromWkt('MULTILINESTRING ((0 1,2 3),(0 1,2 3))')) if lyr.CreateFeature(dst_feat) != 0: gdaltest.post_reason('CreateFeature failed.') return 'fail' dst_feat.Destroy() dst_feat = ogr.Feature(lyr.GetLayerDefn()) dst_feat.SetGeometry( ogr.CreateGeometryFromWkt( 'MULTIPOLYGON (((0 1 0,2 3 0,4 5 0,0 1 0),(0 1 0,2 3 0,4 5 0,0 1 0)),((0 1 0,2 3 0,4 5 0,0 1 0),(0 1 0,2 3 0,4 5 0,0 1 0)))' )) if lyr.CreateFeature(dst_feat) != 0: gdaltest.post_reason('CreateFeature failed.') return 'fail' dst_feat.Destroy() dst_feat = ogr.Feature(lyr.GetLayerDefn()) dst_feat.SetGeometry( ogr.CreateGeometryFromWkt( 'GEOMETRYCOLLECTION (POINT (2 49 1),LINESTRING (0 1,2 3))')) if lyr.CreateFeature(dst_feat) != 0: gdaltest.post_reason('CreateFeature failed.') return 'fail' dst_feat.Destroy() ds.Destroy() return 'success'
def ogr_wkbwkt_test_broken_geom(): list_broken = [ 'POINT', 'POINT UNKNOWN', 'POINT(', 'POINT()', 'POINT(,)', 'POINT(EMPTY', 'POINT(A)', 'POINT(0)', 'POINT(A 0)', 'POINT(0 A)', 'POINT(0 1', 'POINT(0 1,', 'POINT((0 1))', 'POINT Z', 'POINT Z UNKNOWN', 'POINT Z(', 'POINT Z()', 'POINT Z(EMPTY)', 'POINT Z(A)', 'POINT Z(0 1', 'LINESTRING', 'LINESTRING UNKNOWN', 'LINESTRING(', 'LINESTRING()', 'LINESTRING(,)', 'LINESTRING(())', 'LINESTRING(EMPTY', 'LINESTRING(A)', 'LINESTRING(0 1,', 'LINESTRING(0 1,2 3', 'LINESTRING(0 1,,2 3)', 'LINESTRING((0 1,2 3))', 'LINESTRING Z', 'LINESTRING Z UNKNOWN', 'LINESTRING Z(', 'LINESTRING Z()', 'LINESTRING Z(EMPTY)', 'LINESTRING Z(A)', 'LINESTRING Z(0 1', 'LINESTRING Z(0 1,2 3', 'POLYGON', 'POLYGON UNKNOWN', 'POLYGON(', 'POLYGON()', 'POLYGON(,)', 'POLYGON(())', 'POLYGON(EMPTY', 'POLYGON(A)', 'POLYGON(0 1)', 'POLYGON(0 1,2 3', 'POLYGON((0 1,2 3', 'POLYGON((0 1,2 3,', 'POLYGON((0 1,2 3)', 'POLYGON((0 1,2 3),', 'POLYGON((0 1,2 3),EMPTY', 'POLYGON(((0 1,2 3)))', 'POLYGON Z', 'POLYGON Z UNKNOWN', 'POLYGON Z(', 'POLYGON Z()', 'POLYGON Z(EMPTY', 'POLYGON Z(A)', 'POLYGON Z(0 1', 'POLYGON Z(0 1,2 3', 'POLYGON Z((0 1,2 3', 'POLYGON Z((0 1,2 3)', 'POLYGON Z(((0 1,2 3)))', 'MULTIPOINT', 'MULTIPOINT UNKNOWN', 'MULTIPOINT(', 'MULTIPOINT()', 'MULTIPOINT(())', 'MULTIPOINT(EMPTY', 'MULTIPOINT(EMPTY,', 'MULTIPOINT(EMPTY,(0 1)', 'MULTIPOINT(A)', 'MULTIPOINT(0 1', 'MULTIPOINT(0 1,', 'MULTIPOINT(0 1,2 3', 'MULTIPOINT((0 1),,(2 3))', 'MULTIPOINT(0 1,EMPTY', 'MULTIPOINT((0 1),EMPTY', 'MULTIPOINT((0 1)', #'MULTIPOINT(0 1,2 3)', # This one is not SF compliant but supported for legacy 'MULTIPOINT((0 1),(2 3)', 'MULTIPOINT Z', 'MULTIPOINT Z UNKNOWN', 'MULTIPOINT Z(', 'MULTIPOINT Z()', 'MULTIPOINT Z(EMPTY', 'MULTIPOINT Z(A)', 'MULTIPOINT Z(0 1', 'MULTIPOINT Z((0 1)', 'MULTIPOINT Z(0 1,2 3)', 'MULTILINESTRING', 'MULTILINESTRING UNKNOWN', 'MULTILINESTRING(', 'MULTILINESTRING()', 'MULTILINESTRING(,)', 'MULTILINESTRING(())', 'MULTILINESTRING(EMPTY', 'MULTILINESTRING(EMPTY,', 'MULTILINESTRING(A)', 'MULTILINESTRING(0 1', 'MULTILINESTRING(0 1,', 'MULTILINESTRING(0 1,2 3)', 'MULTILINESTRING((0 1,2 3', 'MULTILINESTRING((0 1,2 3),)', 'MULTILINESTRING((0 1)', 'MULTILINESTRING((0 1),', 'MULTILINESTRING((0 1),EMPTY', 'MULTILINESTRING((0 1),(2 3)', 'MULTILINESTRING Z', 'MULTILINESTRING Z UNKNOWN', 'MULTILINESTRING Z(', 'MULTILINESTRING Z()', 'MULTILINESTRING Z(EMPTY', 'MULTILINESTRING Z(A)', 'MULTILINESTRING Z(0 1', 'MULTILINESTRING Z((0 1)', 'MULTILINESTRING Z((0 1),(2 3)', 'MULTIPOLYGON', 'MULTIPOLYGON UNKNOWN', 'MULTIPOLYGON(', 'MULTIPOLYGON()', 'MULTIPOLYGON(,)', 'MULTIPOLYGON(())', 'MULTIPOLYGON((()))', 'MULTIPOLYGON(EMPTY', 'MULTIPOLYGON(EMPTY,', 'MULTIPOLYGON(A)', 'MULTIPOLYGON(0 1', 'MULTIPOLYGON(0 1,', 'MULTIPOLYGON(0 1,2 3)', 'MULTIPOLYGON((0 1,2 3', 'MULTIPOLYGON((0 1,2 3),)', 'MULTIPOLYGON((0 1)', 'MULTIPOLYGON((0 1),', 'MULTIPOLYGON((0 1),EMPTY', 'MULTIPOLYGON((0 1),(2 3)', 'MULTIPOLYGON((0 1),(2 3))', 'MULTIPOLYGON(((0 1))', 'MULTIPOLYGON(((0 1)),', 'MULTIPOLYGON(((0 1)),,', 'MULTIPOLYGON(((0 1),(2 3))', 'MULTIPOLYGON(((0 1),EMPTY', 'MULTIPOLYGON(((0 1),EMPTY,', 'MULTIPOLYGON((((0 1)),)', 'MULTIPOLYGON Z', 'MULTIPOLYGON Z UNKNOWN', 'MULTIPOLYGON Z(', 'MULTIPOLYGON Z()', 'MULTIPOLYGON Z(EMPTY', 'MULTIPOLYGON Z(A)', 'MULTIPOLYGON Z(0 1', 'MULTIPOLYGON Z((0 1)', 'MULTIPOLYGON Z((0 1),(2 3)', 'GEOMETRYCOLLECTION', 'GEOMETRYCOLLECTION UNKNOWN', 'GEOMETRYCOLLECTION(', 'GEOMETRYCOLLECTION()', 'GEOMETRYCOLLECTION(,)', 'GEOMETRYCOLLECTION(())', 'GEOMETRYCOLLECTION(EMPTY', 'GEOMETRYCOLLECTION(EMPTY,', 'GEOMETRYCOLLECTION(A)', 'GEOMETRYCOLLECTION(POINT(0 1)', 'GEOMETRYCOLLECTION(POINT(0 1),', 'GEOMETRYCOLLECTION(POINT(0 1),)', 'GEOMETRYCOLLECTION(POINT(0 1),UNKNOWN)', 'GEOMETRYCOLLECTION Z', 'GEOMETRYCOLLECTION Z(', 'GEOMETRYCOLLECTION Z()', 'GEOMETRYCOLLECTION Z(EMPTY', 'GEOMETRYCOLLECTION Z(POINT(0 1)', ] for wkt in list_broken: gdal.PushErrorHandler('CPLQuietErrorHandler') geom = ogr.CreateGeometryFromWkt(wkt) gdal.PopErrorHandler() if geom is not None: gdaltest.post_reason( 'geom %s instanciated but not expected' % wkt ) return 'fail' return 'success'
def _process_crop(self, sample): """Returns a crop for a specific (internal) set of sampled features.""" # remember: we assume that all rasters have the same intrinsic settings crop_datatype = geo.utils.GDAL2NUMPY_TYPE_CONV[self.rasters_data[0] ["data_type"]] crop_size = (sample["crop_height"], sample["crop_width"], self.rasters_data[0]["band_count"]) crop = np.ma.array(np.zeros(crop_size, dtype=crop_datatype), mask=np.ones(crop_size, dtype=np.uint8)) mask = np.zeros(crop_size[0:2], dtype=np.uint8) crop_raster_gdal = gdal.GetDriverByName("MEM").Create( "", crop_size[1], crop_size[0], crop_size[2], self.rasters_data[0]["data_type"]) crop_raster_gdal.SetGeoTransform(sample["geotransform"]) crop_raster_gdal.SetProjection(self.srs_target.ExportToWkt()) crop_mask_gdal = gdal.GetDriverByName("MEM").Create( "", crop_size[1], crop_size[0], 1, gdal.GDT_Byte) crop_mask_gdal.SetGeoTransform(sample["geotransform"]) crop_mask_gdal.SetProjection(self.srs_target.ExportToWkt()) crop_mask_gdal.GetRasterBand(1).WriteArray( np.zeros(crop_size[0:2], dtype=np.uint8)) ogr_dataset = ogr.GetDriverByName("Memory").CreateDataSource("mask") ogr_layer = ogr_dataset.CreateLayer("feature_mask", srs=self.srs_target) for feature in sample["features"]: ogr_feature = ogr.Feature(ogr_layer.GetLayerDefn()) ogr_geometry = ogr.CreateGeometryFromWkt(feature["geometry"].wkt) ogr_feature.SetGeometry(ogr_geometry) ogr_layer.CreateFeature(ogr_feature) gdal.RasterizeLayer(crop_mask_gdal, [1], ogr_layer, burn_values=[1], options=["ALL_TOUCHED=TRUE"]) np.copyto(dst=mask, src=crop_mask_gdal.GetRasterBand(1).ReadAsArray()) for raster_idx in sample["raster_hits"]: rasterfile = geo.utils.open_rasterfile( self.rasters_data[raster_idx], keep_rasters_open=self.keep_rasters_open) assert rasterfile.RasterCount == crop_size[ 2], "unexpected raster count" # using all cpus should be ok since we probably cant parallelize this loader anyway (swig serialization issues) options = ["NUM_THREADS=ALL_CPUS"] if self.reproj_all_cpus else [] geo.utils.reproject_crop(rasterfile, crop_raster_gdal, crop_size, crop_datatype, reproj_opt=options, fill_nodata=True) for raster_band_idx in range(crop_raster_gdal.RasterCount): curr_band = crop_raster_gdal.GetRasterBand(raster_band_idx + 1) curr_band_array = curr_band.ReadAsArray() flag_mask = curr_band_array != curr_band.GetNoDataValue() np.copyto(dst=crop.data[:, :, raster_band_idx], src=curr_band_array, where=flag_mask) np.bitwise_and(crop.mask[:, :, raster_band_idx], np.invert(flag_mask), out=crop.mask[:, :, raster_band_idx]) # ogr_dataset = None # close local fd # noinspection PyUnusedLocal crop_raster_gdal = None # close local fd # noinspection PyUnusedLocal crop_mask_gdal = None # close local fd # noinspection PyUnusedLocal rasterfile = None # close input fd return crop, mask
def ogr_wkbwkt_test_import_wkt_sf12(): list_wkt_tuples = [ ('POINT EMPTY', 'POINT EMPTY'), ('POINT Z EMPTY', 'POINT EMPTY'), ('POINT M EMPTY', 'POINT EMPTY'), ('POINT ZM EMPTY', 'POINT EMPTY'), ('POINT (0 1)', 'POINT (0 1)'), ('POINT Z (0 1 2)', 'POINT (0 1 2)'), ('POINT M (0 1 2)', 'POINT (0 1)'), ('POINT ZM (0 1 2 3)', 'POINT (0 1 2)'), ('LINESTRING EMPTY', 'LINESTRING EMPTY'), ('LINESTRING Z EMPTY', 'LINESTRING EMPTY'), ('LINESTRING M EMPTY', 'LINESTRING EMPTY'), ('LINESTRING ZM EMPTY', 'LINESTRING EMPTY'), ('LINESTRING (0 1,2 3)', 'LINESTRING (0 1,2 3)'), ('LINESTRING Z (0 1 2,3 4 5)', 'LINESTRING (0 1 2,3 4 5)'), ('LINESTRING M (0 1 2,3 4 5)', 'LINESTRING (0 1,3 4)'), ('LINESTRING ZM (0 1 2 3,4 5 6 7)', 'LINESTRING (0 1 2,4 5 6)'), ('POLYGON EMPTY', 'POLYGON EMPTY'), ('POLYGON (EMPTY)', 'POLYGON EMPTY'), ('POLYGON Z EMPTY', 'POLYGON EMPTY'), ('POLYGON Z (EMPTY)', 'POLYGON EMPTY'), ('POLYGON M EMPTY', 'POLYGON EMPTY'), ('POLYGON ZM EMPTY', 'POLYGON EMPTY'), ('POLYGON ((0 1,2 3,4 5,0 1))', 'POLYGON ((0 1,2 3,4 5,0 1))'), ('POLYGON ((0 1,2 3,4 5,0 1),EMPTY)', 'POLYGON ((0 1,2 3,4 5,0 1))'), ('POLYGON (EMPTY,(0 1,2 3,4 5,0 1))', 'POLYGON EMPTY'), ('POLYGON (EMPTY,(0 1,2 3,4 5,0 1),EMPTY)', 'POLYGON EMPTY'), ('POLYGON Z ((0 1 10,2 3 20,4 5 30,0 1 10),(0 1 10,2 3 20,4 5 30,0 1 10))', 'POLYGON ((0 1 10,2 3 20,4 5 30,0 1 10),(0 1 10,2 3 20,4 5 30,0 1 10))'), ('POLYGON M ((0 1 10,2 3 20,4 5 30,0 1 10))', 'POLYGON ((0 1,2 3,4 5,0 1))'), ('POLYGON ZM ((0 1 10 100,2 3 20 200,4 5 30 300,0 1 10 10))', 'POLYGON ((0 1 10,2 3 20,4 5 30,0 1 10))'), ('MULTIPOINT EMPTY', 'MULTIPOINT EMPTY'), ('MULTIPOINT (EMPTY)', 'MULTIPOINT EMPTY'), ('MULTIPOINT Z EMPTY', 'MULTIPOINT EMPTY'), ('MULTIPOINT Z (EMPTY)', 'MULTIPOINT EMPTY'), ('MULTIPOINT M EMPTY', 'MULTIPOINT EMPTY'), ('MULTIPOINT ZM EMPTY', 'MULTIPOINT EMPTY'), ('MULTIPOINT (0 1,2 3)', 'MULTIPOINT (0 1,2 3)'), # Not SF1.2 compliant but recognized ('MULTIPOINT ((0 1),(2 3))', 'MULTIPOINT (0 1,2 3)'), ('MULTIPOINT ((0 1),EMPTY)', 'MULTIPOINT (0 1)'), # We don't output empty points in multipoint ('MULTIPOINT (EMPTY,(0 1))', 'MULTIPOINT (0 1)'), # We don't output empty points in multipoint ('MULTIPOINT (EMPTY,(0 1),EMPTY)', 'MULTIPOINT (0 1)'), # We don't output empty points in multipoint ('MULTIPOINT Z ((0 1 2),(3 4 5))', 'MULTIPOINT (0 1 2,3 4 5)'), ('MULTIPOINT M ((0 1 2),(3 4 5))', 'MULTIPOINT (0 1,3 4)'), ('MULTIPOINT ZM ((0 1 2 3),(4 5 6 7))', 'MULTIPOINT (0 1 2,4 5 6)'), ('MULTILINESTRING EMPTY', 'MULTILINESTRING EMPTY'), ('MULTILINESTRING (EMPTY)', 'MULTILINESTRING EMPTY'), ('MULTILINESTRING Z EMPTY', 'MULTILINESTRING EMPTY'), ('MULTILINESTRING Z (EMPTY)', 'MULTILINESTRING EMPTY'), ('MULTILINESTRING M EMPTY', 'MULTILINESTRING EMPTY'), ('MULTILINESTRING ZM EMPTY', 'MULTILINESTRING EMPTY'), ('MULTILINESTRING ((0 1,2 3,4 5,0 1))', 'MULTILINESTRING ((0 1,2 3,4 5,0 1))'), ('MULTILINESTRING ((0 1,2 3,4 5,0 1),EMPTY)', 'MULTILINESTRING ((0 1,2 3,4 5,0 1))'), ('MULTILINESTRING (EMPTY,(0 1,2 3,4 5,0 1))', 'MULTILINESTRING ((0 1,2 3,4 5,0 1))'), ('MULTILINESTRING (EMPTY,(0 1,2 3,4 5,0 1),EMPTY)', 'MULTILINESTRING ((0 1,2 3,4 5,0 1))'), ('MULTILINESTRING Z ((0 1 10,2 3 20,4 5 30,0 1 10),(0 1 10,2 3 20,4 5 30,0 1 10))', 'MULTILINESTRING ((0 1 10,2 3 20,4 5 30,0 1 10),(0 1 10,2 3 20,4 5 30,0 1 10))'), ('MULTILINESTRING M ((0 1 10,2 3 20,4 5 30,0 1 10))', 'MULTILINESTRING ((0 1,2 3,4 5,0 1))'), ('MULTILINESTRING ZM ((0 1 10 100,2 3 20 200,4 5 30 300,0 1 10 10))', 'MULTILINESTRING ((0 1 10,2 3 20,4 5 30,0 1 10))'), ('MULTIPOLYGON EMPTY', 'MULTIPOLYGON EMPTY'), ('MULTIPOLYGON (EMPTY)', 'MULTIPOLYGON EMPTY'), ('MULTIPOLYGON Z EMPTY', 'MULTIPOLYGON EMPTY'), ('MULTIPOLYGON Z (EMPTY)', 'MULTIPOLYGON EMPTY'), ('MULTIPOLYGON M EMPTY', 'MULTIPOLYGON EMPTY'), ('MULTIPOLYGON ZM EMPTY', 'MULTIPOLYGON EMPTY'), ('MULTIPOLYGON ((EMPTY))', 'MULTIPOLYGON EMPTY'), ('MULTIPOLYGON (((0 1,2 3,4 5,0 1)))', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1)))'), ('MULTIPOLYGON (((0 1,2 3,4 5,0 1)),((2 3,4 5,6 7,2 3)))', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1)),((2 3,4 5,6 7,2 3)))'), ('MULTIPOLYGON (((0 1,2 3,4 5,0 1),(2 3,4 5,6 7,2 3)))', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1),(2 3,4 5,6 7,2 3)))'), ('MULTIPOLYGON (((0 1,2 3,4 5,0 1)),EMPTY)', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1)))'), ('MULTIPOLYGON (((0 1,2 3,4 5,0 1),EMPTY))', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1)))'), ('MULTIPOLYGON ((EMPTY,(0 1,2 3,4 5,0 1)))', 'MULTIPOLYGON EMPTY'), ('MULTIPOLYGON (((0 1,2 3,4 5,0 1),EMPTY,(2 3,4 5,6 7,2 3)))', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1),(2 3,4 5,6 7,2 3)))'), ('MULTIPOLYGON (((0 1,2 3,4 5,0 1)),((0 1,2 3,4 5,0 1),(2 3,4 5,6 7,2 3)))', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1)),((0 1,2 3,4 5,0 1),(2 3,4 5,6 7,2 3)))'), ('MULTIPOLYGON (EMPTY,((0 1,2 3,4 5,0 1)))', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1)))'), ('MULTIPOLYGON (((0 1,2 3,4 5,0 1)),EMPTY)', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1)))'), ('MULTIPOLYGON Z (((0 1 10,2 3 20,4 5 30,0 1 10)),((0 1 10,2 3 20,4 5 30,0 1 10)))', 'MULTIPOLYGON (((0 1 10,2 3 20,4 5 30,0 1 10)),((0 1 10,2 3 20,4 5 30,0 1 10)))'), ('MULTIPOLYGON M (((0 1 10,2 3 20,4 5 30,0 1 10)))', 'MULTIPOLYGON (((0 1,2 3,4 5,0 1)))'), ('MULTIPOLYGON ZM (((0 1 10 100,2 3 20 200,4 5 30 300,0 1 10 10)))', 'MULTIPOLYGON (((0 1 10,2 3 20,4 5 30,0 1 10)))'), ('GEOMETRYCOLLECTION EMPTY', 'GEOMETRYCOLLECTION EMPTY'), ('GEOMETRYCOLLECTION Z EMPTY', 'GEOMETRYCOLLECTION EMPTY'), ('GEOMETRYCOLLECTION M EMPTY', 'GEOMETRYCOLLECTION EMPTY'), ('GEOMETRYCOLLECTION ZM EMPTY', 'GEOMETRYCOLLECTION EMPTY'), ('GEOMETRYCOLLECTION Z (POINT Z (0 1 2),LINESTRING Z (0 1 2,3 4 5))', 'GEOMETRYCOLLECTION (POINT (0 1 2),LINESTRING (0 1 2,3 4 5))'), ('GEOMETRYCOLLECTION M (POINT M (0 1 2),LINESTRING M (0 1 2,3 4 5))', 'GEOMETRYCOLLECTION (POINT (0 1),LINESTRING (0 1,3 4))'), ('GEOMETRYCOLLECTION ZM (POINT ZM (0 1 2 10),LINESTRING ZM (0 1 2 10,3 4 5 20))', 'GEOMETRYCOLLECTION (POINT (0 1 2),LINESTRING (0 1 2,3 4 5))'), ('GEOMETRYCOLLECTION (POINT EMPTY,LINESTRING EMPTY,POLYGON EMPTY,MULTIPOINT EMPTY,MULTILINESTRING EMPTY,MULTIPOLYGON EMPTY,GEOMETRYCOLLECTION EMPTY)', 'GEOMETRYCOLLECTION (POINT EMPTY,LINESTRING EMPTY,POLYGON EMPTY,MULTIPOINT EMPTY,MULTILINESTRING EMPTY,MULTIPOLYGON EMPTY,GEOMETRYCOLLECTION EMPTY)'), ('GEOMETRYCOLLECTION (POINT Z EMPTY,LINESTRING Z EMPTY,POLYGON Z EMPTY,MULTIPOINT Z EMPTY,MULTILINESTRING Z EMPTY,MULTIPOLYGON Z EMPTY,GEOMETRYCOLLECTION Z EMPTY)', 'GEOMETRYCOLLECTION (POINT EMPTY,LINESTRING EMPTY,POLYGON EMPTY,MULTIPOINT EMPTY,MULTILINESTRING EMPTY,MULTIPOLYGON EMPTY,GEOMETRYCOLLECTION EMPTY)'), # Not SF1.2 compliant but recognized ('GEOMETRYCOLLECTION (POINT(EMPTY),LINESTRING(EMPTY),POLYGON(EMPTY),MULTIPOINT(EMPTY),MULTILINESTRING(EMPTY),MULTIPOLYGON(EMPTY),GEOMETRYCOLLECTION(EMPTY))', 'GEOMETRYCOLLECTION (POINT EMPTY,LINESTRING EMPTY,POLYGON EMPTY,MULTIPOINT EMPTY,MULTILINESTRING EMPTY,MULTIPOLYGON EMPTY,GEOMETRYCOLLECTION EMPTY)'), ] for wkt_tuple in list_wkt_tuples: geom = ogr.CreateGeometryFromWkt(wkt_tuple[0]) if geom is None: gdaltest.post_reason( 'could not instanciate geometry %s' % wkt_tuple[0]) return 'fail' out_wkt = geom.ExportToWkt() if out_wkt != wkt_tuple[1]: gdaltest.post_reason( 'in=%s, out=%s, expected=%s.' % (wkt_tuple[0], out_wkt, wkt_tuple[1]) ) return 'fail' return 'success'
def wkt2geom(wkt): return(ogr.CreateGeometryFromWkt(wkt))
def save_point_list_to_shapefile(class_sample_point_dict, out_path, geotransform, projection_wkt, produce_csv=False): """ :meta private: Parameters ---------- class_sample_point_dict out_path geotransform projection_wkt produce_csv Returns ------- """ log = logging.getLogger(__name__) log.info("Saving point list to shapefile") log.debug("GT: {}\nProjection: {}".format(geotransform, projection_wkt)) driver = ogr.GetDriverByName("ESRI Shapefile") data_source = driver.CreateDataSource(out_path) srs = osr.SpatialReference() srs.ImportFromWkt(projection_wkt) layer = data_source.CreateLayer("validation_points", srs, ogr.wkbPoint) class_field = ogr.FieldDefn("class", ogr.OFTString) class_field.SetWidth(24) layer.CreateField(class_field) for map_class, point_list in class_sample_point_dict.items(): for point in point_list: feature = ogr.Feature(layer.GetLayerDefn()) coord = pyeo.coordinate_manipulation.pixel_to_point_coordinates( point, geotransform) offset = geotransform[ 1] / 2 # Adds half a pixel offset so points end up in the center of pixels wkt = "POINT({} {})".format( coord[0] + offset, coord[1] - offset) # Never forget about negative y values in gts. new_point = ogr.CreateGeometryFromWkt(wkt) feature.SetGeometry(new_point) feature.SetField("class", map_class) layer.CreateFeature(feature) feature = None layer = None data_source = None if produce_csv: csv_out_path = out_path.rsplit('.')[0] + ".csv" with open(csv_out_path, "w") as csv_file: writer = csv.writer(csv_file) writer.writerow(["id", "yCoordinate", "xCoordinate"]) # Join all points create single dimesional list of points (and revise the '*' operator) for id, point in enumerate( itertools.chain(*class_sample_point_dict.values())): coord = pyeo.coordinate_manipulation.pixel_to_point_coordinates( point, geotransform) offset = geotransform[ 1] / 2 # Adds half a pixel offset so points end up in the center of pixels lat = coord[0] + offset lon = coord[1] - offset writer.writerow([id, lon, lat]) log.info("CSV out at: {}".format(csv_out_path))