Make shapefile processing script pick up all shapefiles and fix order of bbox coordinate

This commit is contained in:
TheFGFSEagle
2022-07-30 20:33:29 +02:00
parent dc25bce9b2
commit 016d268c07
3 changed files with 21 additions and 23 deletions
+18 -20
View File
@@ -29,24 +29,25 @@ It will merge / slice the files accordingly with ogr2ogr.
""" """
#As the final step, the resulting shapefiles will be decoded into files that tg-constrcut can read using ogr-decode. #As the final step, the resulting shapefiles will be decoded into files that tg-constrcut can read using ogr-decode.
#""" #"""
catnames = ["buildings", "landuse", "natural", "places", "pofw", "pois", "railways", "roads", "traffic", "transport", "water", "waterways"] catnames = ["buildings_a", "landuse_a", "natural", "natural_a", "places", "places_a", "pofw", "pofw_a", "pois", "pois_a", "railways", "roads", "traffic", "traffic_a", "transport", "transport_a", "water_a", "waterways"]
tmpdir = tempfile.TemporaryDirectory() tmpdir = tempfile.TemporaryDirectory()
def find(src): def find(src):
osm_shapefiles = [] osm_shapefiles = []
files = glob.glob(os.path.join(src, "**", "**.shp")) files = glob.glob(os.path.join(src, "**", "**.shp"))
for file in files: for file in files:
if "osm" in os.path.split(file): if "osm" in os.path.split(file)[-1]:
osm_shapefiles.append(file) osm_shapefiles.append(file)
return sorted(osm_shapefiles) return sorted(osm_shapefiles)
def categorize(shapefiles): def categorize(shapefiles):
categorized = [] categorized = dict((catname, []) for catname in catnames)
for shapefile in shapefiles: for shapefile in shapefiles:
name = os.path.split(shapefile)[-1].split(".")[0] name = os.path.split(shapefile)[-1].split(".")[0]
name = re.sub(r"gis|osm|a|free|_|(1-9)", "", name) # gis_osm_landuse_a_free_1 becomes just landuse name = re.sub(r"gis|osm|free|_(?!a)|\d", "", name) # gis_osm_landuse_a_free_1 becomes just landuse_a
# Skip if the name is not a recognized catname # Skip if the name is not a recognized catname
if not name in catnames: if not name in catnames:
continue continue
@@ -83,9 +84,12 @@ def get_extents(categorized):
def merge_slice(shapefiles, coords, dest): def merge_slice(shapefiles, coords, dest):
for category in shapefiles: for category in shapefiles:
files = ['"' + shapefile["path"] + '"' for shapefile in shapefiles[category]] print(f"Merging and slicing {category}")
cmd = f'ogr2ogr -f "ESRI Shapefile" {os.path.join(dest, category + ".shp")} {" ".join(files)} -clipsrc {coords["xll"]} {coords["yll"]} {coords["xur"]} {coords["yur"]} -progress -single -lco ENCODING=UTF-8' dest_file = os.path.join(dest, category + ".shp")
subprocess.run(cmd, shell=True) os.path.isfile(dest_file) and os.remove(dest_file)
for shapefile in shapefiles[category]:
cmd = f'ogr2ogr -f "ESRI Shapefile" "{dest_file}" "{shapefile["path"]}" -clipsrc {coords["xll"]} {coords["yll"]} {coords["xur"]} {coords["yur"]} -progress -lco ENCODING=UTF-8 -append'
subprocess.run(cmd, shell=True)
#def decode(shapefiles, dest): #def decode(shapefiles, dest):
# pass # pass
@@ -119,15 +123,11 @@ if __name__ == "__main__":
) )
argp.add_argument( argp.add_argument(
"-l", "--lower-left", "-b", "--bbox",
help="coordinates of the lower left corner of the bounding box of the region that shapefiles should be processed for (default: %(default)s)", help="coordinates of the lbounding box of the region that shapefiles should be processed for (default: %(default)s)",
default="-180,-90" default=[-180, -90, 180, 90],
) nargs=4,
type=float
argp.add_argument(
"-u", "--upper-right",
help="coordinates of the upper-right corner of the bounding box of the region that shapefiles should be processed for (default: %(default)s)",
default="180,90"
) )
args = argp.parse_args() args = argp.parse_args()
@@ -138,9 +138,7 @@ if __name__ == "__main__":
src = args.input_folder src = args.input_folder
dest = args.output_folder dest = args.output_folder
xll, yll = args.lower_left.split(",") coords = dict(zip(["xll", "yll", "xur", "yur"], args.bbox))
xur, yur = args.upper_right.split(",")
coords = {"xll": xll, "yll": yll, "xur": xur, "yur": yur}
if not os.path.isdir(src): if not os.path.isdir(src):
print(f"ERROR: input folder {src} does not exist, exiting") print(f"ERROR: input folder {src} does not exist, exiting")
@@ -154,4 +152,4 @@ if __name__ == "__main__":
#extents = get_extents(categorized) #extents = get_extents(categorized)
#shapefiles = merge_slice(extents, coords, dest) #shapefiles = merge_slice(extents, coords, dest)
shapefiles = merge_slice(categories, coords, dest) shapefiles = merge_slice(categories, coords, dest)
#result = shapefiles.decode(shapefiles, dest) #result = decode(shapefiles, dest)
+1 -1
View File
@@ -34,7 +34,7 @@ def filter_airports_list(airports_count, airports_list, icaos, bbox):
if icaos and any(icao in (airport.get("AirportCode", None), airport_metadata.get("icao_code", None)) for icao in icaos): if icaos and any(icao in (airport.get("AirportCode", None), airport_metadata.get("icao_code", None)) for icao in icaos):
airports_list_filtered.append(airport) airports_list_filtered.append(airport)
elif bbox: elif bbox:
if bbox[0] < airport["Latitude"] < bbox[2] and bbox[1] < airport["Longitude"] < bbox[3]: if bbox[1] < airport["Latitude"] < bbox[3] and bbox[0] < airport["Longitude"] < bbox[2]:
airports_list_filtered.append(airport) airports_list_filtered.append(airport)
print("Filtering airports … done ") print("Filtering airports … done ")
+2 -2
View File
@@ -24,9 +24,9 @@ def get_fg_tile_span(lat):
return 0.5 return 0.5
elif lat >= -83: elif lat >= -83:
return 1 return 1
elif lat >= -86 elif lat >= -86:
return 2 return 2
elif lat >= -89 elif lat >= -89:
return 4 return 4
else: else:
return 12 return 12