-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathBasemap.java
More file actions
382 lines (325 loc) · 14 KB
/
Basemap.java
File metadata and controls
382 lines (325 loc) · 14 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package com.protomaps.basemap;
import blue.strategic.parquet.ParquetReader;
import com.onthegomap.planetiler.ForwardingProfile;
import com.onthegomap.planetiler.Planetiler;
import com.onthegomap.planetiler.config.Arguments;
import com.onthegomap.planetiler.reader.parquet.GeoParquetMetadata;
import com.onthegomap.planetiler.util.Downloader;
import com.protomaps.basemap.feature.CountryCoder;
import com.protomaps.basemap.feature.QrankDb;
import com.protomaps.basemap.layers.Boundaries;
import com.protomaps.basemap.layers.Buildings;
import com.protomaps.basemap.layers.Earth;
import com.protomaps.basemap.layers.Landcover;
import com.protomaps.basemap.layers.Landuse;
import com.protomaps.basemap.layers.Places;
import com.protomaps.basemap.layers.Pois;
import com.protomaps.basemap.layers.Roads;
import com.protomaps.basemap.layers.Transit;
import com.protomaps.basemap.layers.Water;
import com.protomaps.basemap.postprocess.Clip;
import com.protomaps.basemap.text.FontRegistry;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.parquet.ParquetReadOptions;
import org.apache.parquet.hadoop.ParquetFileReader;
import org.locationtech.jts.geom.Envelope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Basemap extends ForwardingProfile {
private static final Logger LOGGER = LoggerFactory.getLogger(Basemap.class);
public Basemap(QrankDb qrankDb, CountryCoder countryCoder, Clip clip,
String layer) {
if (layer.isEmpty() || layer.equals(Boundaries.LAYER_NAME)) {
var admin = new Boundaries();
registerHandler(admin);
registerSourceHandler("osm", admin::processOsm);
registerSourceHandler("ne", admin::processNe);
}
if (layer.isEmpty() || layer.equals(Buildings.LAYER_NAME)) {
var buildings = new Buildings();
registerHandler(buildings);
registerSourceHandler("osm", buildings::processOsm);
registerSourceHandler("pm:overture", buildings::processOverture);
}
if (layer.isEmpty() || layer.equals(Landuse.LAYER_NAME)) {
var landuse = new Landuse();
registerHandler(landuse);
registerSourceHandler("osm", landuse::processOsm);
registerSourceHandler("pm:overture", landuse::processOverture);
}
if (layer.isEmpty() || layer.equals(Landcover.LAYER_NAME)) {
var landcover = new Landcover();
registerHandler(landcover);
registerSourceHandler("landcover", landcover::processLandcover);
registerSourceHandler("ne", landcover::processNe);
registerSourceHandler("pm:overture", landcover::processOverture);
}
if (layer.isEmpty() || layer.equals(Places.LAYER_NAME)) {
var place = new Places(countryCoder);
registerHandler(place);
registerSourceHandler("osm", place::processOsm);
registerSourceHandler("pm:overture", place::processOverture);
}
if (layer.isEmpty() || layer.equals(Pois.LAYER_NAME)) {
var poi = new Pois(qrankDb);
registerHandler(poi);
registerSourceHandler("osm", poi::processOsm);
registerSourceHandler("pm:overture", poi::processOverture);
}
if (layer.isEmpty() || layer.equals(Roads.LAYER_NAME)) {
var roads = new Roads(countryCoder);
registerHandler(roads);
registerSourceHandler("osm", roads::processOsm);
registerSourceHandler("pm:overture", roads::processOverture);
}
if (layer.isEmpty() || layer.equals(Transit.LAYER_NAME)) {
var transit = new Transit();
registerHandler(transit);
registerSourceHandler("osm", transit::processOsm);
}
if (layer.isEmpty() || layer.equals(Water.LAYER_NAME)) {
var water = new Water();
registerHandler(water);
registerSourceHandler("osm", water::processOsm);
registerSourceHandler("osm_water", water::processPreparedOsm);
registerSourceHandler("ne", water::processNe);
registerSourceHandler("pm:overture", water::processOverture);
}
if (layer.isEmpty() || layer.equals(Earth.LAYER_NAME)) {
var earth = new Earth();
registerHandler(earth);
registerSourceHandler("osm", earth::processOsm);
registerSourceHandler("osm_land", earth::processPreparedOsm);
registerSourceHandler("ne", earth::processNe);
registerSourceHandler("pm:overture", earth::processOverture);
}
if (clip != null) {
registerHandler(clip);
}
}
@Override
public String name() {
return "Protomaps Basemap";
}
@Override
public String description() {
return "Basemap layers derived from OpenStreetMap and Natural Earth";
}
@Override
public String version() {
return "4.14.5";
}
@Override
public boolean isOverlay() {
return false;
}
@Override
public String attribution() {
return """
<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap</a>
""".trim();
}
@Override
public Map<String, String> extraArchiveMetadata() {
Map<String, String> result = new HashMap<>();
FontRegistry fontRegistry = FontRegistry.getInstance();
List<String> scripts = fontRegistry.getScripts();
for (String script : scripts) {
result.put("pgf:" + script.toLowerCase() + ":name", fontRegistry.getName(script));
result.put("pgf:" + script.toLowerCase() + ":version", fontRegistry.getVersion(script));
}
return result;
}
/**
* Extracts bounds from GeoParquet metadata and returns them as a formatted string.
*
* @param parquetPath Path to the GeoParquet file
* @return Bounds string in format "minX,minY,maxX,maxY" if successful, empty otherwise
*/
static java.util.Optional<String> extractBoundsFromGeoParquet(Path parquetPath) {
try {
var inputFile = ParquetReader.makeInputFile(parquetPath.toFile());
try (var reader = ParquetFileReader.open(inputFile, ParquetReadOptions.builder().build())) {
var fileMetadata = reader.getFooter().getFileMetaData();
var geoparquet = GeoParquetMetadata.parse(fileMetadata);
Envelope bounds = geoparquet.primaryColumnMetadata().envelope();
if (bounds != null && !bounds.isNull() && bounds.getArea() > 0) {
String boundsStr = String.format("%f,%f,%f,%f",
bounds.getMinX(), bounds.getMinY(), bounds.getMaxX(), bounds.getMaxY());
return java.util.Optional.of(boundsStr);
}
}
} catch (Exception e) {
LOGGER.warn("Failed to read bounds from GeoParquet file: {}", e.getMessage());
}
return java.util.Optional.empty();
}
public static void main(String[] args) throws IOException {
// Check for help flag
for (String arg : args) {
if ("--version".equals(arg) || "-v".equals(arg)) {
printVersion();
System.exit(0);
}
if ("--help".equals(arg) || "-h".equals(arg)) {
printHelp();
System.exit(0);
}
}
run(Arguments.fromArgsOrConfigFile(args));
}
private static void printVersion() {
Basemap basemap = new Basemap(null, null, null, "");
System.out.println(basemap.version());
}
private static void printHelp() {
Basemap basemap = new Basemap(null, null, null, "");
System.out.println(String.format("""
%s v%s
%s
Usage:
java -jar protomaps-basemap-HEAD-with-deps.jar [options]
Protomaps Basemap Options:
--help, -h Show this help message and exit
--area=<name> Geofabrik area name to download, or filename in data/sources/
(default: monaco, e.g., us/california, washington)
--overture=<path> Path to Overture Maps Parquet file (mutually exclusive with --area)
--maxzoom=<n> Maximum zoom level (default: 15)
--layer=<name> Process only a single layer (optional)
Valid values: boundaries, buildings, landuse, landcover,
places, pois, roads, transit, water, earth
--clip=<path> GeoJSON file path to clip tileset (optional)
--clip-buffer=<n> Relative buffer around clip polygon (default: 4.0/256.0)
Use 0 for exact boundary, e.g. --clip-buffer=0
Common Planetiler Options:
--output=<path> Output file path and format (e.g., output.pmtiles)
--force Overwrite existing output file
--download Automatically download input sources
--only-download Exit after downloading sources
--osm-path=<path> Path to existing OSM extract file
Examples:
java -jar protomaps-basemap-HEAD-with-deps.jar --area=monaco
java -jar protomaps-basemap-HEAD-with-deps.jar --area=boston --maxzoom=14 --force
java -jar protomaps-basemap-HEAD-with-deps.jar --layer=roads --area=seattle
For a complete list of Planetiler options, see:
https://github.com/onthegomap/planetiler#usage
For more information: https://github.com/protomaps/basemaps
""", basemap.name(), basemap.version(), basemap.description()));
}
@java.lang.SuppressWarnings("java:S5738")
static void run(Arguments args) throws IOException {
args = args.orElse(Arguments.of("maxzoom", 15));
Path dataDir = Path.of("data");
Path sourcesDir = dataDir.resolve("sources");
var countryCoder = CountryCoder.fromJarResource();
String area = args.getString("area", "Geofabrik area name to download, or filename in data/sources/", "");
String overtureFile = args.getString("overture", "Path to Overture Maps Parquet file", "");
if (!area.isEmpty() && !overtureFile.isEmpty()) {
LOGGER.error("Error: Cannot specify both --area and --overture");
System.exit(1);
}
if (area.isEmpty() && overtureFile.isEmpty()) {
area = "monaco"; // default
}
// Extract bounds from GeoParquet metadata if using Overture source
if (!overtureFile.isEmpty() && args.getString("bounds", "", "").isEmpty()) {
Path parquetPath = Path.of(overtureFile);
var boundsOpt = extractBoundsFromGeoParquet(parquetPath);
if (boundsOpt.isPresent()) {
String boundsStr = boundsOpt.get();
LOGGER.info("Setting bounds from GeoParquet metadata: {}", boundsStr);
args = args.orElse(Arguments.of("bounds", boundsStr));
}
}
var planetiler = Planetiler.create(args)
.addNaturalEarthSource("ne", sourcesDir.resolve("natural_earth_vector.sqlite.zip"),
"https://naciscdn.org/naturalearth/packages/natural_earth_vector.sqlite.zip");
if (!overtureFile.isEmpty()) {
// Add Overture Parquet source
planetiler.addParquetSource("pm:overture",
List.of(Path.of(overtureFile)),
false, // not Hive partitioned dirname, just a single file
fields -> fields.get("id"),
fields -> fields.get("type") // source layer
);
} else {
// Add OSM and GeoPackage sources
planetiler
.addOsmSource("osm", Path.of("data", "sources", area + ".osm.pbf"), "geofabrik:" + area)
.addShapefileSource("osm_water", sourcesDir.resolve("water-polygons-split-3857.zip"),
"https://osmdata.openstreetmap.de/download/water-polygons-split-3857.zip")
.addShapefileSource("osm_land", sourcesDir.resolve("land-polygons-split-3857.zip"),
"https://osmdata.openstreetmap.de/download/land-polygons-split-3857.zip")
.addGeoPackageSource("landcover", sourcesDir.resolve("daylight-landcover.gpkg"),
"https://r2-public.protomaps.com/datasets/daylight-landcover.gpkg");
}
Path pgfEncodingZip = sourcesDir.resolve("pgf-encoding.zip");
Path qrankCsv = sourcesDir.resolve("qrank.csv.gz");
if (!Files.exists(qrankCsv)) {
Downloader.create(planetiler.config())
.add("qrank", "https://qrank.toolforge.org/download/qrank.csv.gz", qrankCsv)
.run();
}
var qrankDb = QrankDb.fromCsv(qrankCsv);
if (!Files.exists(pgfEncodingZip)) {
Downloader.create(planetiler.config())
.add("pgf-encoding", "https://wipfli.github.io/pgf-encoding/pgf-encoding.zip", pgfEncodingZip)
.run();
}
FontRegistry fontRegistry = FontRegistry.getInstance();
fontRegistry.setZipFilePath(pgfEncodingZip.toString());
Clip clip = null;
double clipBuffer = args.getDouble("clip_buffer",
"Relative buffer around clip polygon. 0 for exact boundary.", Clip.DEFAULT_BUFFER);
if (clipBuffer < 0) {
LOGGER.error("Error: --clip-buffer must be >= 0, but was {}", clipBuffer);
System.exit(1);
}
var clipArg = args.getString("clip", "File path to GeoJSON Polygon or MultiPolygon geometry to clip tileset.", "");
if (!clipArg.isEmpty()) {
clip =
Clip.fromGeoJSONFile(args.getStats(), planetiler.config().minzoom(), planetiler.config().maxzoom(), true,
clipBuffer, Paths.get(clipArg));
}
List<String> availableLayers = List.of(
Boundaries.LAYER_NAME,
Buildings.LAYER_NAME,
Landuse.LAYER_NAME,
Landcover.LAYER_NAME,
Places.LAYER_NAME,
Pois.LAYER_NAME,
Roads.LAYER_NAME,
Transit.LAYER_NAME,
Water.LAYER_NAME,
Earth.LAYER_NAME
);
String layer = args.getString("layer",
"Process only a single layer. Possible values are: " + String.join(", ", availableLayers), "");
if (!(layer.isEmpty() || availableLayers.contains(layer))) {
LOGGER.error("Error: --layer={} is not a valid option. Possible values are: {}", layer,
String.join(", ", availableLayers));
System.exit(1);
}
fontRegistry.loadFontBundle("NotoSansDevanagari-Regular", "1", "Devanagari");
String outputName;
if (!overtureFile.isEmpty()) {
String filename = Path.of(overtureFile).getFileName().toString();
if (filename.endsWith(".parquet")) {
outputName = filename.substring(0, filename.length() - ".parquet".length());
} else {
outputName = filename;
}
} else {
outputName = area;
}
planetiler.setProfile(new Basemap(qrankDb, countryCoder, clip, layer))
.setOutput(Path.of(outputName + ".pmtiles"))
.run();
}
}