Skip to content

Commit dcaacf2

Browse files
committed
Refactor create_polys and to shapefile pipeline.
1 parent c3e15ff commit dcaacf2

12 files changed

+85
-96
lines changed

handler.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
import cv2
22
import timeit
3-
import glob
43
import argparse
54
import os
65
import multiprocessing as mp
76
mp.set_start_method('spawn', force=True)
8-
import sys
9-
import resource
107
import numpy as np
118
import raster_processing
12-
import to_agol
9+
from utils import to_shapefile
10+
from utils import to_agol
11+
from utils import features
1312
import rasterio.warp
1413
import torch
1514
#import ray
1615
from collections import defaultdict
1716
from os import makedirs, path
18-
from functools import partial
1917
from pathlib import Path
20-
from shapely.geometry import mapping
2118
from torch.utils.data import DataLoader
22-
from yacs.config import CfgNode
2319
from skimage.morphology import square, dilation
2420
from tqdm import tqdm
2521

2622

2723
from dataset import XViewDataset
2824
from models import XViewFirstPlaceLocModel, XViewFirstPlaceClsModel
2925

30-
import functools
3126
import logging
32-
import struct
33-
import sys
3427

3528
logger = logging.getLogger()
3629

@@ -541,10 +534,11 @@ def main():
541534
# Get files for creating shapefile and/or pushing to AGOL
542535
if args.create_shapefile or agol_push.get('dmg'):
543536
dmg_files = get_files(Path(args.output_directory) / 'dmg')
537+
polygons = features.create_polys(dmg_files)
544538

545539
if args.create_shapefile:
546540
print('Creating shapefile')
547-
raster_processing.create_shapefile(dmg_files,
541+
to_shapefile.create_shapefile(polygons,
548542
Path(args.output_directory).joinpath('shapes') / 'damage.shp',
549543
args.destination_crs)
550544

raster_processing.py

-39
Original file line numberDiff line numberDiff line change
@@ -218,42 +218,3 @@ def get_tiles(ds, width, height):
218218
chips.append(outpath.resolve())
219219

220220
return chips
221-
222-
223-
def create_shapefile(in_files, out_shapefile, dest_crs):
224-
225-
"""
226-
Create shapefiles from input dmg files
227-
:param in_files: Files to process for shapefile.
228-
:param out_shapefile: File to write shapefile.
229-
:param dest_crs: Destination crs.
230-
:return: None
231-
"""
232-
233-
polygons = []
234-
for idx, f in enumerate(in_files):
235-
src = rasterio.open(f)
236-
crs = src.crs
237-
transform = src.transform
238-
239-
bnd = src.read(1)
240-
polys = list(shapes(bnd, transform=transform))
241-
242-
for geom, val in polys:
243-
if val == 0:
244-
continue
245-
polygons.append((Polygon(shape(geom)), val))
246-
247-
shp_schema = {
248-
'geometry': 'Polygon',
249-
'properties': {'dmg': 'int'}
250-
}
251-
252-
# Write out all the multipolygons to the same file
253-
with fiona.open(out_shapefile, 'w', 'ESRI Shapefile', shp_schema,
254-
dest_crs) as shp:
255-
for polygon, px_val in polygons:
256-
shp.write({
257-
'geometry': mapping(polygon),
258-
'properties': {'dmg': int(px_val)}
259-
})

tests/__init__.py

Whitespace-only changes.

tests/test_handler.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import unittest
1+
from unittest import TestCase
22
import handler
33

44

5-
class TestGetFiles(unittest.TestCase):
5+
class TestGetFiles(TestCase):
66

77
def test_get_files(self):
88
self.path = 'data/input/pre'
99
self.result = handler.get_files(self.path)
1010
self.assertEqual(4, len(self.result))
1111

1212

13-
class TestReprojectionHelper(unittest.TestCase):
13+
class TestReprojectionHelper(TestCase):
1414

1515
pass
1616

1717

18-
class TestPostprocessAndWrite(unittest.TestCase):
18+
class TestPostprocessAndWrite(TestCase):
1919

2020
pass
2121

2222

23-
class TestFilesClass(unittest.TestCase):
23+
class TestFilesClass(TestCase):
2424

2525
pass
2626

tests/test_inference.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import unittest
1+
from unittest import TestCase
22

33
import inference
44

55

6-
class TestInference(unittest.TestCase):
6+
class TestInference(TestCase):
77

88
def setUp(self):
99
self.opts = inference.Options('sample_data/input/pre')

tests/test_raster_processing.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unittest
1+
from unittest import TestCase
22
import os
33
import rasterio
44
from pathlib import Path
@@ -7,7 +7,7 @@
77
import handler
88

99

10-
class TestReproject(unittest.TestCase):
10+
class TestReproject(TestCase):
1111

1212
def test_reproject(self):
1313
self.in_file = Path('data/input/pre/tile_337-10160.tif')
@@ -17,7 +17,7 @@ def test_reproject(self):
1717
self.assertEqual('EPSG:4326', self.test)
1818

1919

20-
class TestCreateMosaic(unittest.TestCase):
20+
class TestCreateMosaic(TestCase):
2121

2222
def setUp(self):
2323
self.files = handler.get_files(Path('data/input/pre'))
@@ -41,7 +41,7 @@ def test_correct_extent(self):
4141
self.assertEqual((366642.60000000003, 4104511.1999999997), self.src.transform * (0, 0))
4242

4343

44-
class TestGetIntersect(unittest.TestCase):
44+
class TestGetIntersect(TestCase):
4545

4646
def test_intersect_extent(self):
4747
self.test = raster_processing.get_intersect(
@@ -69,10 +69,7 @@ def test_intersect_extent(self):
6969
# self.result = raster_processing.get_intersect_win(self.mosaic, self.intersect)
7070
# self.assertEqual(self.test, self.result)
7171

72-
class TestCreateShapefile(unittest.TestCase):
7372

74-
pass
75-
76-
class TestCheckDims(unittest.TestCase):
73+
class TestCheckDims(TestCase):
7774

7875
pass

tests/test_to_agol.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
import unittest
2-
import to_agol
1+

tests/test_to_shapefile.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from utils import to_shapefile
2+
from unittest import TestCase
3+
from utils.features import create_polys
4+
from handler import get_files
5+
from pathlib import Path
6+
7+
8+
class Test(TestCase):
9+
10+
def test_create_shapefile(self):
11+
self.files = get_files(Path('data/output/dmg'))
12+
self.polys = create_polys(self.files)
13+
self.shapefile = to_shapefile(self.polys, Path('~/Downloads'), 'EPSG:4326')

utils/__init__.py

Whitespace-only changes.

utils/features.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import rasterio
2+
from rasterio.features import shapes
3+
from shapely.geometry import Polygon, shape
4+
5+
def create_polys(in_files):
6+
7+
"""
8+
Create palygons to use for feature creation.
9+
:param in_files: DMG files to create polygons from.
10+
:return: Polygons from dmg files.
11+
"""
12+
13+
polygons = []
14+
for idx, f in enumerate(in_files):
15+
src = rasterio.open(f)
16+
crs = src.crs
17+
transform = src.transform
18+
19+
bnd = src.read(1)
20+
polys = list(shapes(bnd, transform=transform))
21+
22+
for geom, val in polys:
23+
if val == 0:
24+
continue
25+
polygons.append((Polygon(shape(geom)), val))
26+
27+
return polygons

to_agol.py utils/to_agol.py

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import arcgis
2-
import rasterio
3-
import shapely.geometry
42
from tqdm import tqdm
5-
from rasterio.features import shapes
6-
from shapely.geometry import Polygon, shape, MultiPolygon
3+
from shapely.geometry import MultiPolygon
74

85

96

@@ -45,31 +42,6 @@ def agol_arg_check(args):
4542
return True
4643

4744

48-
def create_polys(in_files):
49-
# TODO: Combine this with raster_processing.create_shapefile polygon creation.
50-
"""
51-
Create palygons to use for feature creation.
52-
:param in_files: DMG files to create polygons from.
53-
:return: Polygons from dmg files.
54-
"""
55-
56-
polygons = []
57-
for idx, f in enumerate(in_files):
58-
src = rasterio.open(f)
59-
crs = src.crs
60-
transform = src.transform
61-
62-
bnd = src.read(1)
63-
polys = list(shapes(bnd, transform=transform))
64-
65-
for geom, val in polys:
66-
if val == 0:
67-
continue
68-
polygons.append((Polygon(shape(geom)), val))
69-
70-
return polygons
71-
72-
7345
def create_aoi_poly(features):
7446

7547
"""

utils/to_shapefile.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import fiona
2+
from shapely.geometry import mapping
3+
4+
def create_shapefile(polygons, out_shapefile, dest_crs):
5+
6+
"""
7+
Create shapefile from input polygons
8+
:param polygons: Polygons to export as shapefile.
9+
:param out_shapefile: Destination shapefile.
10+
:param dest_crs: Destination CRS.
11+
:return: None
12+
"""
13+
14+
shp_schema = {
15+
'geometry': 'Polygon',
16+
'properties': {'dmg': 'int'}
17+
}
18+
19+
# Write out all the multipolygons to the same file
20+
with fiona.open(out_shapefile, 'w', 'ESRI Shapefile', shp_schema,
21+
dest_crs) as shp:
22+
for polygon, px_val in polygons:
23+
shp.write({
24+
'geometry': mapping(polygon),
25+
'properties': {'dmg': int(px_val)}
26+
})

0 commit comments

Comments
 (0)