-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgeoutils.py
117 lines (98 loc) · 4.76 KB
/
geoutils.py
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
###############################################################################
#### Collection of useful functions
####
#### Licensed under MIT License
###############################################################################
#
# Copyright (c) 2023 Martin Christen, martin.christen@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
###############################################################################
from urllib.request import urlopen
import os
import sys
import json
######## shortcut for downloads ###################################################################
geodata = {"natural-earth": "http://naciscdn.org/naturalearth/packages/natural_earth_vector.gpkg.zip",
"natural-earth-raster-shaded": "https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/raster/NE1_HR_LC_SR_W_DR.zip",
"landsat-ch-25": "https://data.geo.admin.ch/ch.swisstopo.images-landsat25/data.zip",
"swissimage-25": "https://data.geo.admin.ch/ch.swisstopo.swissimage-25/data.zip",
"geonames": "https://download.geonames.org/export/dump/cities500.zip",
"bluemarble-jp2": "https://data.geopython.xyz/bluemarble/world.jp2",
"BFS": "https://www.bfs.admin.ch/bfsstatic/dam/assets/11947559/master",
"ourairports": "https://ourairports.com/data/airports.csv"}
###################################################################################################
def download(url, destfile, overwrite=True):
print("Downloading", destfile, "from", url)
if os.path.exists(destfile) and not overwrite:
print("File already exists, not overwriting.")
return
response = urlopen(url)
info = response.info()
cl = info["Content-Length"]
if cl != None:
filesize = int(cl)
currentsize = 0
with open(destfile, 'wb') as f:
while True:
chunk = response.read(16*1024)
currentsize += len(chunk)
if not chunk:
break
f.write(chunk)
percent = int(100*currentsize/filesize)
bar = "*"*(percent)
bar += "-"*((100-percent))
print('\r{}% done \t[{}]'.format(percent, bar), end='')
print("")
else:
print("Downloading please wait... (filesize unknown)")
with open(destfile, 'wb') as f:
while True:
chunk = response.read(16*1024)
if not chunk:
break
f.write(chunk)
#------------------------------------------------------------------------------
# Convert a OpenStreetMap Polygon JSON to GeoJSON
# just a quick hack
def osm2geojson(s):
geojson = {
"type": "Feature",
"geometry" : {},
"properties": {},
}
data = json.loads(s)
for key in data:
if key == "geojson":
geojson["geometry"] = data["geojson"]
else:
d = geojson["properties"]
d[key] = data[key]
return json.dumps(geojson)
#------------------------------------------------------------------------------
# Fix DLL/so path in notebooks. This is required sometimes, but not often...
# For proj4 we need to set the PROJ_LIB path manually
# Bug: https://github.com/jupyter/notebook/issues/4569
def fixenv():
os.environ['PATH'] = os.path.split(sys.executable)[0] + "/Library/bin" + os.pathsep + os.environ['PATH']
if 'PROJ_LIB' not in os.environ:
os.environ['PROJ_LIB'] = os.path.split(sys.executable)[0] + "/library/share/proj"
os.environ['GDAL_DATA'] = os.path.split(sys.executable)[0] + "/../../../share/gdal/"
#------------------------------------------------------------------------------