summaryrefslogtreecommitdiff
path: root/regenerate_index_html.py
blob: de0eab7111c43c77c40f295374a2a266df7c7d85 (plain)
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
from osgeo import gdal, ogr
import glob
import os
import json
import subprocess

cdn_url = 'https://cdn.proj.org'

agency_list = json.loads(open('AGENCY.json','rt').read())
agencies = {}
for item in agency_list:
    agencies[item['id']] = item


dirnames = []
links = []
for dirname in glob.glob('*'):
    if not os.path.isdir(dirname):
        continue
    dirnames.append(dirname)

gj_ds = ogr.GetDriverByName('GeoJSON').CreateDataSource('files.geojson')
lyr = gj_ds.CreateLayer('files')
lyr.CreateField(ogr.FieldDefn('url', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('name', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('area_of_use', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('type', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('source', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('source_country', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('source_id', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('source_url', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('description', ogr.OFTString))

total_size = 0
set_files = set()
for dirname in sorted(dirnames):
    if '_' not in dirname:
        continue
    filenames = []
    readme_filename = None
    for f in glob.glob(dirname + '/*'):
        f = os.path.basename(f)
        if f.startswith('README'):
            assert not readme_filename
            readme_filename = f
        else:
            filenames.append(f)

    title = '%s' % (dirname)
    try:
        agency = agencies[dirname]
        title = '<a href="%s">%s</a>' % (agency['url'].replace('&', "&amp;"), agency['agency'])
    except KeyError:

        pass

    links.append('</ul><hr><h3>%s</h3><ul>' % title )
    for f in [readme_filename] + sorted(filenames):

        assert f not in set_files
        set_files.add(f)

        full_filename = os.path.join(dirname, f)
        ds = gdal.OpenEx(full_filename)
        desc = ''
        area_of_use = ''
        if ds:
            imageDesc = ds.GetMetadataItem('TIFFTAG_IMAGEDESCRIPTION')
            if imageDesc:
                pos = imageDesc.find('. Converted from')
                if pos >= 0:
                    imageDesc = imageDesc[0:pos]
                desc = imageDesc

            feat = ogr.Feature(lyr.GetLayerDefn())
            feat['url'] = cdn_url + '/' + f
            feat['name'] = f
            type = ds.GetMetadataItem('TYPE')
            if type:
                feat['type'] = type
            area_of_use = ds.GetMetadataItem('area_of_use')
            if area_of_use:
                feat['area_of_use'] = area_of_use
            feat['source'] = agency['agency']
            feat['source_country'] = agency['country']
            feat['source_id'] = agency['id']
            feat['source_url'] = agency['url']
            if imageDesc:
                feat['description'] = imageDesc
            gt = ds.GetGeoTransform()
            xmin = gt[0] + 0.5 * gt[1]
            ymax = gt[3] + 0.5 * gt[5]
            xmax = xmin + gt[1] * (ds.RasterXSize - 1)
            ymin = ymax + gt[5] * (ds.RasterYSize - 1)

            subds_list = ds.GetSubDatasets()
            if subds_list:
                for subds_name, _ in subds_list:
                    ds = gdal.Open(subds_name)
                    gt = ds.GetGeoTransform()
                    xmin_subds = gt[0] + 0.5 * gt[1]
                    ymax_subds = gt[3] + 0.5 * gt[5]
                    xmax_subds = xmin_subds + gt[1] * (ds.RasterXSize - 1)
                    ymin_subds = ymax_subds + gt[5] * (ds.RasterYSize - 1)
                    xmin = min(xmin, xmin_subds)
                    ymin = min(ymin, ymin_subds)
                    xmax = max(xmax, xmax_subds)
                    ymax = max(ymax, ymax_subds)

            geom = ogr.Geometry(ogr.wkbPolygon)
            ring = ogr.Geometry(ogr.wkbLinearRing)
            ring.AddPoint_2D(xmin, ymin)
            ring.AddPoint_2D(xmin, ymax)
            ring.AddPoint_2D(xmax, ymax)
            ring.AddPoint_2D(xmax, ymin)
            ring.AddPoint_2D(xmin, ymin)
            geom.AddGeometry(ring)
            feat.SetGeometry(geom)
            lyr.CreateFeature(feat)

        size_str = ''
        size = os.stat(full_filename).st_size
        total_size += size
        if size > 1024 * 1024:
            size_str = '. Size: %.1f MB' % (size / (1024. * 1024))

        if f.startswith('README'):
            last_modified = ''
        else:
            p = subprocess.run(['git','log','-1','--pretty=format:%cd','--date=short',full_filename], check=True, stdout=subprocess.PIPE)
            last_modified = '. Last modified: ' + p.stdout.decode('ascii')

        if area_of_use:
            area_of_use = ' - ' + area_of_use
        else:
            area_of_use = ''

        if desc:
            desc = ' - ' + desc
        else:
            desc = ''

        links.append('<li><a href="%s">%s</a>%s%s%s%s</li>' % (f, f, area_of_use, desc, size_str, last_modified))

total_size_str = '%d MB' % (total_size // (1024 * 1024))

content = '<!-- This is a generated file by regenerate_index_html.py. Do not modify !!!! Modify index.html.in instead if you need to make changes-->\n\n'
content += open('index.html.in', 'rt').read().replace('${LINKS_WILL_BE_ADDED_HERE_BY_REGENERATE_INDEX_HTML}', '\n'.join(links)).replace('${TOTAL_SIZE}', total_size_str)
open('index.html', 'wt').write(content)