Skip to content

Commit 89d5d28

Browse files
committed
gdal raster tile: add --kml option
1 parent b3605c3 commit 89d5d28

10 files changed

+746
-17
lines changed

apps/gdalalg_raster_tile.cpp

Lines changed: 440 additions & 17 deletions
Large diffs are not rendered by default.

apps/gdalalg_raster_tile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class GDALRasterTileAlgorithm final : public GDALAlgorithm
6363
bool m_auxXML = false;
6464
bool m_resume = false;
6565
int m_numThreads = 0;
66+
bool m_kml = false;
6667

6768
std::vector<std::string> m_webviewers{};
6869
std::string m_url{};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<kml xmlns="http://www.opengis.net/kml/2.2">
3+
<Document>
4+
<name>10/177/409.kml</name>
5+
<description></description>
6+
<Style>
7+
<ListStyle id="hideChildren">
8+
<listItemType>checkHideChildren</listItemType>
9+
</ListStyle>
10+
</Style>
11+
<Region>
12+
<LatLonAltBox>
13+
<north>34.0162418897</north>
14+
<south>33.7243396617</south>
15+
<east>-117.4218750000</east>
16+
<west>-117.7734375000</west>
17+
</LatLonAltBox>
18+
<Lod>
19+
<minLodPixels>128</minLodPixels>
20+
<maxLodPixels>2048</maxLodPixels>
21+
</Lod>
22+
</Region>
23+
<GroundOverlay>
24+
<drawOrder>20</drawOrder>
25+
<Icon>
26+
<href>409.png</href>
27+
</Icon>
28+
<LatLonBox>
29+
<north>34.0162418897</north>
30+
<south>33.7243396617</south>
31+
<east>-117.4218750000</east>
32+
<west>-117.7734375000</west>
33+
</LatLonBox>
34+
</GroundOverlay>
35+
<NetworkLink>
36+
<name>11/354/818.png</name>
37+
<Region>
38+
<LatLonAltBox>
39+
<north>34.0162418897</north>
40+
<south>33.8704155509</south>
41+
<east>-117.5976562500</east>
42+
<west>-117.7734375000</west>
43+
</LatLonAltBox>
44+
<Lod>
45+
<minLodPixels>128</minLodPixels>
46+
<maxLodPixels>-1</maxLodPixels>
47+
</Lod>
48+
</Region>
49+
<Link>
50+
<href>../../11/354/818.kml</href>
51+
<viewRefreshMode>onRegion</viewRefreshMode>
52+
<viewFormat/>
53+
</Link>
54+
</NetworkLink>
55+
</Document>
56+
</kml>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<kml xmlns="http://www.opengis.net/kml/2.2">
3+
<Document>
4+
<name>11/354/818.kml</name>
5+
<description></description>
6+
<Style>
7+
<ListStyle id="hideChildren">
8+
<listItemType>checkHideChildren</listItemType>
9+
</ListStyle>
10+
</Style>
11+
<Region>
12+
<LatLonAltBox>
13+
<north>34.0162418897</north>
14+
<south>33.8704155509</south>
15+
<east>-117.5976562500</east>
16+
<west>-117.7734375000</west>
17+
</LatLonAltBox>
18+
<Lod>
19+
<minLodPixels>128</minLodPixels>
20+
<maxLodPixels>-1</maxLodPixels>
21+
</Lod>
22+
</Region>
23+
<GroundOverlay>
24+
<drawOrder>22</drawOrder>
25+
<Icon>
26+
<href>818.png</href>
27+
</Icon>
28+
<LatLonBox>
29+
<north>34.0162418897</north>
30+
<south>33.8704155509</south>
31+
<east>-117.5976562500</east>
32+
<west>-117.7734375000</west>
33+
</LatLonBox>
34+
</GroundOverlay>
35+
</Document>
36+
</kml>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<kml xmlns="http://www.opengis.net/kml/2.2">
3+
<Document>
4+
<name></name>
5+
<description></description>
6+
<Style>
7+
<ListStyle id="hideChildren">
8+
<listItemType>checkHideChildren</listItemType>
9+
</ListStyle>
10+
</Style>
11+
<NetworkLink>
12+
<name>10/177/409.png</name>
13+
<Region>
14+
<LatLonAltBox>
15+
<north>34.0162418897</north>
16+
<south>33.7243396617</south>
17+
<east>-117.4218750000</east>
18+
<west>-117.7734375000</west>
19+
</LatLonAltBox>
20+
<Lod>
21+
<minLodPixels>128</minLodPixels>
22+
<maxLodPixels>-1</maxLodPixels>
23+
</Lod>
24+
</Region>
25+
<Link>
26+
<href>10/177/409.kml</href>
27+
<viewRefreshMode>onRegion</viewRefreshMode>
28+
<viewFormat/>
29+
</Link>
30+
</NetworkLink>
31+
</Document>
32+
</kml>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
3+
<Document>
4+
<name>0/0/0.kml</name>
5+
<description></description>
6+
<Style>
7+
<ListStyle id="hideChildren">
8+
<listItemType>checkHideChildren</listItemType>
9+
</ListStyle>
10+
</Style>
11+
<Region>
12+
<LatLonAltBox>
13+
<north>33.9013544773</north>
14+
<south>33.7620761093</south>
15+
<east>-117.4742770366</east>
16+
<west>-117.6411699152</west>
17+
</LatLonAltBox>
18+
<Lod>
19+
<minLodPixels>128</minLodPixels>
20+
<maxLodPixels>-1</maxLodPixels>
21+
</Lod>
22+
</Region>
23+
<GroundOverlay>
24+
<drawOrder>1</drawOrder>
25+
<Icon>
26+
<href>0.png</href>
27+
</Icon>
28+
<LatLonBox>
29+
<north>33.9013544773</north>
30+
<south>33.7620761093</south>
31+
<east>-117.4742770366</east>
32+
<west>-117.6411699152</west>
33+
</LatLonBox>
34+
<gx:LatLonQuad><coordinates>-117.6401365334,33.7620761093 -117.4742770366,33.7628248474 -117.4750426978,33.9013544773 -117.6411699152,33.9006018373</coordinates></gx:LatLonQuad>
35+
</GroundOverlay>
36+
</Document>
37+
</kml>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<kml xmlns="http://www.opengis.net/kml/2.2">
3+
<Document>
4+
<name></name>
5+
<description></description>
6+
<Style>
7+
<ListStyle id="hideChildren">
8+
<listItemType>checkHideChildren</listItemType>
9+
</ListStyle>
10+
</Style>
11+
<NetworkLink>
12+
<name>0/0/0.png</name>
13+
<Region>
14+
<LatLonAltBox>
15+
<north>33.9013544773</north>
16+
<south>33.7620761093</south>
17+
<east>-117.4742770366</east>
18+
<west>-117.6411699152</west>
19+
</LatLonAltBox>
20+
<Lod>
21+
<minLodPixels>128</minLodPixels>
22+
<maxLodPixels>-1</maxLodPixels>
23+
</Lod>
24+
</Region>
25+
<Link>
26+
<href>0/0/0.kml</href>
27+
<viewRefreshMode>onRegion</viewRefreshMode>
28+
<viewFormat/>
29+
</Link>
30+
</NetworkLink>
31+
</Document>
32+
</kml>

autotest/utilities/test_gdalalg_raster_tile.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,3 +1445,109 @@ def test_gdalalg_raster_tile_raster_min_max_zoom(tmp_vsimem):
14451445
assert ds.ReadRaster(0, 0, 200, 100, band_list=[1, 2, 3]) == src_ds.ReadRaster(
14461446
buf_xsize=200, buf_ysize=100
14471447
)
1448+
1449+
1450+
def test_gdalalg_raster_tile_raster_kml(tmp_vsimem):
1451+
1452+
alg = get_alg()
1453+
alg["input"] = gdal.Translate(
1454+
"", "../gcore/data/byte.tif", format="MEM", outputSRS="EPSG:32611"
1455+
)
1456+
alg["output"] = tmp_vsimem
1457+
alg["min-zoom"] = 10
1458+
alg["max-zoom"] = 11
1459+
alg["resampling"] = "nearest"
1460+
alg["kml"] = True
1461+
with gdal.config_option("GDAL_RASTER_TILE_KML_PREC", "10"):
1462+
assert alg.Run()
1463+
1464+
assert gdal.ReadDirRecursive(tmp_vsimem) == [
1465+
"10/",
1466+
"10/177/",
1467+
"10/177/409.kml",
1468+
"10/177/409.png",
1469+
"11/",
1470+
"11/354/",
1471+
"11/354/818.kml",
1472+
"11/354/818.png",
1473+
"doc.kml",
1474+
"leaflet.html",
1475+
"mapml.mapml",
1476+
"openlayers.html",
1477+
]
1478+
1479+
with gdal.VSIFile(tmp_vsimem / "doc.kml", "rb") as f:
1480+
got = f.read()
1481+
# Uncomment below line to regenerate expected file
1482+
# open("data/gdal_raster_tile_expected_byte_10_11_doc.kml", "wb").write(got)
1483+
assert (
1484+
got
1485+
== open("data/gdal_raster_tile_expected_byte_10_11_doc.kml", "rb").read()
1486+
)
1487+
1488+
with gdal.VSIFile(tmp_vsimem / "10" / "177" / "409.kml", "rb") as f:
1489+
got = f.read()
1490+
# Uncomment below line to regenerate expected file
1491+
# open("data/gdal_raster_tile_expected_byte_10_11_10_177_409.kml", "wb").write(got)
1492+
assert (
1493+
got
1494+
== open(
1495+
"data/gdal_raster_tile_expected_byte_10_11_10_177_409.kml", "rb"
1496+
).read()
1497+
)
1498+
1499+
with gdal.VSIFile(tmp_vsimem / "11" / "354" / "818.kml", "rb") as f:
1500+
got = f.read()
1501+
# Uncomment below line to regenerate expected file
1502+
# open("data/gdal_raster_tile_expected_byte_10_11_11_354_818.kml", "wb").write(got)
1503+
assert (
1504+
got
1505+
== open(
1506+
"data/gdal_raster_tile_expected_byte_10_11_11_354_818.kml", "rb"
1507+
).read()
1508+
)
1509+
1510+
if gdal.GetDriverByName("KMLSuperOverlay"):
1511+
ds = gdal.Open(tmp_vsimem / "doc.kml")
1512+
assert ds.GetRasterBand(1).Checksum() == 4215
1513+
1514+
1515+
def test_gdalalg_raster_tile_raster_kml_with_gx_latlonquad(tmp_vsimem):
1516+
1517+
alg = get_alg()
1518+
alg["input"] = gdal.Translate(
1519+
"", "../gcore/data/byte.tif", format="MEM", outputSRS="EPSG:32611"
1520+
)
1521+
alg["output"] = tmp_vsimem
1522+
alg["tiling-scheme"] = "raster"
1523+
alg["resampling"] = "nearest"
1524+
alg["kml"] = True
1525+
with gdal.config_option("GDAL_RASTER_TILE_KML_PREC", "10"):
1526+
assert alg.Run()
1527+
1528+
assert gdal.ReadDirRecursive(tmp_vsimem) == [
1529+
"0/",
1530+
"0/0/",
1531+
"0/0/0.kml",
1532+
"0/0/0.png",
1533+
"doc.kml",
1534+
"openlayers.html",
1535+
]
1536+
1537+
with gdal.VSIFile(tmp_vsimem / "doc.kml", "rb") as f:
1538+
got = f.read()
1539+
# Uncomment below line to regenerate expected file
1540+
# open("data/gdal_raster_tile_expected_byte_raster_doc.kml", "wb").write(got)
1541+
assert (
1542+
got
1543+
== open("data/gdal_raster_tile_expected_byte_raster_doc.kml", "rb").read()
1544+
)
1545+
1546+
with gdal.VSIFile(tmp_vsimem / "0" / "0" / "0.kml", "rb") as f:
1547+
got = f.read()
1548+
# Uncomment below line to regenerate expected file
1549+
# open("data/gdal_raster_tile_expected_byte_raster_0_0_0.kml", "wb").write(got)
1550+
assert (
1551+
got
1552+
== open("data/gdal_raster_tile_expected_byte_raster_0_0_0.kml", "rb").read()
1553+
)

doc/source/programs/gdal_raster_tile.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ Standard options
175175

176176
Generate .aux.xml sidecar files when needed
177177

178+
.. option:: --kml
179+
180+
Generate Google Earth SuperOverlay metadata.
181+
Not compatible with tiling schemes with non-power-of-two zoom levels.
182+
178183
.. option:: --resume
179184

180185
Generate only missing files. Can be used when interrupting a previous run

port/cpl_known_config_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ constexpr static const char* const apszKnownConfigOptions[] =
369369
"GDAL_PROXY_AUTH", // from cpl_http.cpp
370370
"GDAL_PYTHON_DRIVER_PATH", // from gdalpythondriverloader.cpp
371371
"GDAL_RASTER_TILE_HTML_PREC", // from gdalalg_raster_tile.cpp
372+
"GDAL_RASTER_TILE_KML_PREC", // from gdalalg_raster_tile.cpp
372373
"GDAL_RASTERIO_RESAMPLING", // from gdal_misc.cpp
373374
"GDAL_RB_FLUSHBLOCK_SLEEP_AFTER_DROP_LOCK", // from gdalrasterblock.cpp
374375
"GDAL_RB_FLUSHBLOCK_SLEEP_AFTER_RB_LOCK", // from gdalrasterblock.cpp

0 commit comments

Comments
 (0)