favyen commited on
Commit
c68d083
1 Parent(s): 326a402

Add script to visualize tiles.

Browse files
Files changed (2) hide show
  1. README.md +4 -0
  2. visualize_tile.py +95 -0
README.md CHANGED
@@ -89,6 +89,10 @@ Then launch Python shell:
89
  # These group together many 1.25 m/pixel 512x512 tiles into one tar file.
90
  print(f"{epsg_code}_{col//512//32}_{row//512//32}")
91
 
 
 
 
 
92
 
93
  Sentinel-2
94
  ----------
 
89
  # These group together many 1.25 m/pixel 512x512 tiles into one tar file.
90
  print(f"{epsg_code}_{col//512//32}_{row//512//32}")
91
 
92
+ So then you would download the tar file from the second prefix, extract it, and look at the file with name matching the first prefix.
93
+
94
+ See visualize_tile.py for example of visualizing the data at a particular tile.
95
+
96
 
97
  Sentinel-2
98
  ----------
visualize_tile.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This script visualizes a tile.
3
+ Example usage:
4
+ python visualize_tile.py 32610_859_-8247 /path/to/dataset/ /path/to/output/
5
+ """
6
+
7
+ import json
8
+ import os
9
+ import sys
10
+
11
+ import numpy as np
12
+ from PIL import Image
13
+ import rasterio
14
+ import rasterio.features
15
+
16
+ label = sys.argv[1]
17
+ root_dir = sys.argv[2]
18
+ out_dir = sys.argv[3]
19
+
20
+ # Landsat.
21
+ with rasterio.open(os.path.join(root_dir, "landsat", f"{label}_8.tif")) as src:
22
+ array = src.read()
23
+ for idx in range(16):
24
+ img = np.clip((array[idx, :, :].astype(np.float64) - 5000) / 20, 0, 255).astype(np.uint8)
25
+ img = img.repeat(axis=0, repeats=8).repeat(axis=1, repeats=8)
26
+ Image.fromarray(img).save(os.path.join(out_dir, f"{label}_landsat{idx}.png"))
27
+
28
+ # NAIP.
29
+ array = np.array(Image.open(os.path.join(root_dir, "naip", f"{label}.png")))
30
+ Image.fromarray(array[:, :, 0:3]).save(os.path.join(out_dir, f"{label}_naip.png"))
31
+
32
+ # Old NAIP.
33
+ array = np.array(Image.open(os.path.join(root_dir, "oldnaip", f"{label}.png")))
34
+ Image.fromarray(array[:, :, 0:3]).save(os.path.join(out_dir, f"{label}_oldnaip.png"))
35
+
36
+ # OpenStreetMap.
37
+ with open(os.path.join(root_dir, "openstreetmap", f"{label}.geojson")) as f:
38
+ data = json.load(f)
39
+ category_colors = {
40
+ "river": [0, 0, 255],
41
+ "road": [255, 255, 255],
42
+ "building": [255, 255, 0],
43
+ "parking": [255, 0, 0],
44
+ "leisure_park": [144, 238, 144],
45
+ "solar": [128, 128, 128],
46
+ }
47
+ category_selectors = {
48
+ "leisure_park": lambda feat: feat["properties"]["category"] == "leisure" and feat["properties"].get("leisure") == "park",
49
+ "solar": lambda feat: feat["properties"]["category"] == "power_plant" and feat["properties"].get("plant:source") == "solar",
50
+ }
51
+ img = np.zeros((512, 512, 3), dtype=np.uint8)
52
+ for category, color in category_colors.items():
53
+ selector = category_selectors.get(category, lambda feat: feat["properties"]["category"] == category)
54
+ geometries = [feat["geometry"] for feat in data["features"] if selector(feat)]
55
+ if len(geometries) == 0:
56
+ continue
57
+ mask = rasterio.features.rasterize(geometries, out_shape=(512, 512))
58
+ img[mask > 0] = color
59
+ Image.fromarray(img).save(os.path.join(out_dir, f"{label}_openstreetmap.png"))
60
+
61
+ # Sentinel-1.
62
+ with rasterio.open(os.path.join(root_dir, "sentinel1", f"{label}.tif")) as src:
63
+ array = src.read()
64
+ img = np.clip((array[0, :, :] + 20) * 10, 0, 255).astype(np.uint8)
65
+ img = img.repeat(axis=0, repeats=8).repeat(axis=1, repeats=8)
66
+ Image.fromarray(img).save(os.path.join(out_dir, f"{label}_sentinel1.png"))
67
+
68
+ # Sentinel-2.
69
+ with rasterio.open(os.path.join(root_dir, "sentinel2", f"{label}_8.tif")) as src:
70
+ array = src.read()
71
+ for idx in range(8):
72
+ img = np.clip(array[(idx*4+2, idx*4+1, idx*4+0), :, :].transpose(1, 2, 0) / 10, 0, 255).astype(np.uint8)
73
+ img = img.repeat(axis=0, repeats=8).repeat(axis=1, repeats=8)
74
+ Image.fromarray(img).save(os.path.join(out_dir, f"{label}_sentinel2_{idx}.png"))
75
+
76
+ # WorldCover.
77
+ array = np.array(Image.open(os.path.join(root_dir, "worldcover", f"{label}.png")))
78
+ img = np.zeros((512, 512, 3), dtype=np.uint8)
79
+ category_colors = {
80
+ 10: [0, 100, 0],
81
+ 20: [255, 187, 34],
82
+ 30: [255, 255, 76],
83
+ 40: [240, 150, 255],
84
+ 50: [250, 0, 0],
85
+ 60: [180, 180, 180],
86
+ 70: [240, 240, 240],
87
+ 80: [0, 100, 200],
88
+ 90: [0, 150, 160],
89
+ 95: [0, 207, 117],
90
+ 100: [250, 230, 160],
91
+ }
92
+ for category, color in category_colors.items():
93
+ mask = (array == category).repeat(axis=0, repeats=8).repeat(axis=1, repeats=8)
94
+ img[mask] = color
95
+ Image.fromarray(img).save(os.path.join(out_dir, f"{label}_worldcover.png"))