Orthoimage_download_script 3.23 KB
Newer Older
Mayer's avatar
Mayer committed
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
from owslib.wms import WebMapService

# Initialisiere Web Mapping Service fuer das Land Bayern
wms = WebMapService('https://geoservices.bayern.de/od/wms/dop/v1/dop20?')from owslib.wms import WebMapService

# Definiere eine Funktion, welche RGBI-Daten herunterlaedt und speichert, basierend auf einer bounding box (Gauss-Kruger zone 4 Koordinaten)
def get_wms_tile(bbox, 
                 gsd=0.2,
                 url='https://geoservices.bayern.de/od/wms/dop/v1/dop20?', 
                 srs='EPSG:31468', 
                 format='image/tiff',
                 targetdir='./'):
    
    # open wms
    wms = WebMapService(url)

    # determine image shape in pixels
    width = int((bbox[2]-bbox[0])/0.2)
    height = int((bbox[3]-bbox[1])/0.2)

    # retrieve RGB data
    params = {
        'layers': ["by_dop20c"],
        'bbox': bbox,
        'srs': srs,
        'format': format,
        'size': (width, height)
        }
    data = wms.getmap(**params, timeout=60)  # Timeout auf 60 Sekunden erhöhen
    rgb_data = np.array(Image.open(data).convert('RGB'))

    # retrieve IRG data
    params = {
        'layers': ["by_dop20cir"],
        'bbox': bbox,
        'srs': srs,
        'format': format,
        'size': (width, height)
        }
    data = wms.getmap(**params, timeout=60)  # Timeout auf 60 Sekunden erhöhen
    irg_data = np.array(Image.open(data).convert('RGB'))

    # combine datasets
    rgbi_data = np.array([rgb_data[:,:,0], rgb_data[:,:,1], rgb_data[:,:,2], irg_data[:,:,0]], dtype=np.uint8)
    # shape: [channels, height, width]

    # check image properties
    assert np.all(np.equal(rgbi_data.shape, (4, height, width)))

    # write data to geotiff file
    transform = from_bounds(*bbox, rgbi_data.shape[2], rgbi_data.shape[1])

    filename = "{:d}_{:d}_rgbi_20cm.tif".format(int(bbox[0]), int(bbox[1]))
    with rasterio.open(
                os.path.join(targetdir, filename),
                'w',
                driver='GTiff',
                height=height,
                width=width,
                count=rgbi_data.shape[0],
                dtype='uint8',
                crs=CRS.from_string(srs),
                transform=transform) as dataset:
        dataset.write(rgbi_data)

    return rgbi_data




# Download fuer Stadt Memmingen

import os

wue_bbox = (4365264, 5315180, 4365944, 5320884)
# W, S, N, E

width = 200  # px
height = 200 # px
gsd = 0.2 # m/px

os.mkdir('memmingen/') if not os.path.exists('memmingen') else None

for x in tqdm(np.arange(wue_bbox[0], wue_bbox[2], width*gsd)):
    for y in np.arange(wue_bbox[1], wue_bbox[3], height*gsd):
        
        coo = (x, y) # SW corner of tile
        bb = (x, y, x+width*gsd, y+height*gsd) # (x_min, y_min, x_max, y_max)
        
        get_wms_tile(bb, targetdir='memmingen/')





# Umwandlung der (RGBI) TIFF-Files in (RGB) PNGs
from PIL import Image

datadir = 'wuerzburg/'
targetdir = 'wuerzburg_rgb/'
filenames = os.listdir(datadir)

os.mkdir(targetdir) if not os.path.exists(targetdir) else None

for filename in filenames:
    dataset = rasterio.open(os.path.join(datadir, filename))

    img = np.dstack(dataset.read()[:3])
    image = Image.fromarray(img.astype('uint8'), 'RGB')
    image.save(os.path.join(targetdir, filename.replace('.tif', '_rgb.png')))

    print(filename)