Skip to content

Commit 361a069

Browse files
committed
Change API to pass only index data corresponding to a given sheet in the constructor
1 parent eccc90e commit 361a069

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A Python utility for processing topographic maps.
55

66
## Description
77

8-
This project provides a collection of command-line tools and a Python library to process, analyze, and manipulate topographic map data, specifically for creating web-mappable tiles from GeoTIFF files. The tools allow you to create tiles, partition large tile sets, and update existing tile sets efficiently.
8+
This project provides a collection of command-line tools and a Python library to process, georeference, and export topographic map data in raster form, specifically for creating web-mappable tiles from GeoTIFF files. The tools allow you to create tiles, partition large tile sets, and update existing tile sets efficiently.
99

1010
## Installation
1111

@@ -60,7 +60,7 @@ The main steps, executed by the `processor.process()` method, are as follows:
6060
1. **Image Preparation (`rotate`):** To minimize computation, the processor first creates a smaller, shrunken version of the full-resolution map. On this small image, it locates the main contour of the map's frame (the border area). By calculating the angle of this contour's minimum bounding box, it determines the skew of the map. The full-resolution image is then rotated using this angle to make the frame perfectly horizontal, correcting for any tilt introduced during scanning.
6161
2. **Corner Detection (`get_corners`):** Once the image is straightened, the processor uses the same map frame contour to identify the four corner regions. By narrowing the search to these smaller areas, it can apply more precise logic to find the exact pixel coordinates of the map's neatline corners. This is a critical step that often requires custom implementation in a subclass (`get_intersection_point`) to handle the specific visual features of the map's corners (e.g., crosses, specific colors, patterns).
6262
3. **Grid Line Removal (`remove_grid_lines`):** If configured, the processor can detect and remove grid lines (graticules) from the map image. This is achieved by implementing the `locate_grid_lines` method. The removal helps create a cleaner final map by inpainting the areas where the lines were.
63-
4. **Georeferencing (`georeference`):** Using the detected corner coordinates and the corresponding real-world geographic coordinates (provided via the `index_map`), the tool creates Ground Control Points (GCPs). It then uses `gdal_translate` to create an initial georeferenced GeoTIFF.
63+
4. **Georeferencing (`georeference`):** Using the detected corner coordinates and the corresponding real-world geographic coordinates (provided via the `index_box`), the tool creates Ground Control Points (GCPs). It then uses `gdal_translate` to create an initial georeferenced GeoTIFF.
6464
5. **Warping and Cutting (`warp`):** The georeferenced image is then warped into the standard `EPSG:3857` projection used by most web maps. During this step, it's also precisely cropped to the map's boundary (neatline) using a cutline derived from the corner points. The output is a clean, map-only image, correctly projected.
6565
6. **Exporting (`export`):** The final warped image is converted into a Cloud-Optimized GeoTIFF (COG), which is highly efficient for web-based tiling. A corresponding bounds file (`.geojsonl`) is also created, defining the geographic extent of the map sheet.
6666

@@ -70,15 +70,15 @@ The output of this library workflow—a directory of COG `.tif` files and a dire
7070

7171
The `TopoMapProcessor` class provides a framework for processing individual map sheets. To use it, you need to create a subclass and implement the required methods.
7272

73-
Here's an example of how to use the `TopoMapProcessor` class, based on the provided `parse_jica.py` script:
73+
Here's an example of how to use the `TopoMapProcessor` class.
7474

7575
```python
7676
from topo_map_processor import TopoMapProcessor, LineRemovalParams
7777

7878
class SampleProcessor(TopoMapProcessor):
7979

80-
def __init__(self, filepath, extra, index_map):
81-
super().__init__(filepath, extra, index_map)
80+
def __init__(self, filepath, extra, index_box, index_props):
81+
super().__init__(filepath, extra, index_box, index_props)
8282
# ... (additional initialization)
8383

8484
def get_inter_dir(self):
@@ -109,14 +109,16 @@ def process_files():
109109
# ... (load image files and special cases)
110110
for filepath in image_files:
111111
extra = special_cases.get(filepath.name, {})
112-
index_map = get_index_map_jica()
113-
processor = SampleProcessor(filepath, extra, index_map)
112+
index_box, index_props = get_index_data(filepath.name) # This should return the corresponding index box and properties for the file
113+
processor = SampleProcessor(filepath, extra, index_box, index_props)
114114
processor.process()
115115
```
116116

117117
The library is configurable and most parameters can be overridden at a per sheet level using the `extra` dictionary.
118118

119-
The `index_map` is used to map specific file names to their corresponding metadata, which can be useful for processing specific datasets. In particular the index_map is expected to contain the geojson geometry of the map sheet in the `geometry` key, which is used to create the bounds file and georeference the sheet. The geometry is expected to be in counter clockwise order starting at the top left corner of the map sheet.
119+
The `index_box` is used to provide the coordinates of the corners of the sheet. This information is used to create the bounds file and georeference the sheet. This is expected to be a list of coordinates in counter clockwise order starting at the top left corner of the map sheet.
120+
121+
The optional `index_properties` dictionary can contain additional metadata about the sheet, such as its name, description, and other properties which end up in the generated bounds files.
120122

121123
The end result is a Cloud optimized GeoTIFF file, a bounds file in GeoJSON format from which the tiles can be generated, using the `tile` command-line tool, and a set of tiles in the specified directory.
122124

topo_map_processor/__init__.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ def rowcol(self, x, y):
7070

7171

7272
class TopoMapProcessor:
73-
def __init__(self, filepath, extra, index_map):
73+
def __init__(self, filepath, extra, index_box, index_properties={}):
7474
self.filepath = filepath
75-
self.index_map = index_map
75+
self.index_box = index_box
76+
self.index_properties = index_properties
7677

7778
self.SHOW_IMG = os.getenv('SHOW_IMG', '0') == '1'
7879
self.INSPECT = os.getenv('INSPECT', '0') == '1'
@@ -141,11 +142,8 @@ def get_id(self):
141142
ext = self.filepath.suffix
142143
return self.filepath.name.replace(ext, '')
143144

144-
def get_sheet_ibox(self, sheet_id=None):
145-
if sheet_id is None:
146-
sheet_id = self.get_id()
147-
148-
return self.index_map[sheet_id]
145+
def get_sheet_ibox(self):
146+
return self.index_box
149147

150148
def get_data_dir(self):
151149
return Path('data')
@@ -1607,12 +1605,15 @@ def georeference(self):
16071605
def get_cutline_props(self):
16081606
crs_proj = self.get_crs_proj()
16091607

1610-
props = {
1608+
props = {}
1609+
props.update(self.index_properties)
1610+
1611+
props.update({
16111612
'id': self.get_id(),
16121613
'crs': crs_proj,
16131614
'gcps': self.get_gcps(pre_rotated=True),
16141615
'pixel_cutline': self.get_full_pixel_cutline(pre_rotated=True),
1615-
}
1616+
})
16161617
return props
16171618

16181619
def create_cutline(self, ibox, file):

0 commit comments

Comments
 (0)