forked from safelease/Pix2Poly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmass_roads_clip_tile_vectors.py
More file actions
65 lines (52 loc) · 1.86 KB
/
mass_roads_clip_tile_vectors.py
File metadata and controls
65 lines (52 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from osgeo import gdal
import os
import subprocess
from tqdm import tqdm
from multiprocessing import Pool
def clip_shapefile(paths_dict):
# get the extent of the raster
raster_path = paths_dict['raster_path']
save_path = paths_dict['save_path']
vector_path = paths_dict['vector_path']
src = gdal.Open(raster_path)
ulx, xres, xskew, uly, yskew, yres = src.GetGeoTransform()
sizeX = src.RasterXSize * xres
sizeY = src.RasterYSize * yres
lrx = ulx + sizeX
lry = uly + sizeY
src = None
# format the extent coords
extent = f"{ulx} {lry} {lrx} {uly}"
# print(extent)
# make clip command with ogr2ogr
cmd = f"ogr2ogr {save_path} {vector_path} -clipsrc {extent}"
# call the command
subprocess.call(cmd, shell=True)
return 0
def main():
split = "train" # "train" or "valid" or "test"
data_root = f"../data/mass_roads_1500"
vector_dir = os.path.join(data_root, split, "shape")
save_dir = f'../data/mass_roads_224/{split}/shape'
os.makedirs(save_dir, exist_ok=True)
rasters_dir = f'../data/mass_roads_224/{split}/images/'
rasters = os.listdir(rasters_dir)
param_inputs = []
for raster in rasters:
raster_path = os.path.join(rasters_dir, raster)
save_path = os.path.join(save_dir, raster.split('.')[0]+".geojson")
raster_info = raster.split('_')
vec_desc = f"{raster_info[0]}_{raster_info[1]}.geojson"
vector_path = os.path.join(vector_dir, vec_desc)
param_inputs.append(
{
'raster_path': raster_path,
'vector_path':vector_path,
'save_path': save_path
}
)
# clip_shapefile(raster_path, vector_path, save_path)
with Pool() as p:
_ = list(tqdm(p.imap(clip_shapefile, param_inputs), total=len(param_inputs)))
if __name__ == "__main__":
main()