-
Notifications
You must be signed in to change notification settings - Fork 0
/
rasterize_shapefiles.py
136 lines (121 loc) · 4.59 KB
/
rasterize_shapefiles.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
""" This script rasterizes all the spatial datasets needed by data_qdgc """
import os
import geopandas as gpd
import pandas as pd
import pyogrio
import rasterio
from rasterio import features
from tifffile import imwrite
DATA_FOLDER = "./data/"
TEMP_FOLDER = "./tmp/"
DATABASE_FOLDER = "./database_files/"
out_shape = (21600, 43200)
print("\n\n>> Preparing realm data from ocean dataset")
try:
os.system(
f"unzip -o {TEMP_FOLDER}MarineRealmsShapeFile -d {TEMP_FOLDER}MarineRealms"
)
dat = gpd.read_file(f"{TEMP_FOLDER}MarineRealms/MarineRealms.shp")
shapes = dat[["geometry", "Realm"]].values.tolist()
road_zones_arr = rasterio.features.rasterize(
shapes,
fill=-1,
out_shape=out_shape,
transform=rasterio.transform.from_bounds(
-180, -90, 180, 90, out_shape[1], out_shape[0]
),
)
imwrite(TEMP_FOLDER + "dat_realms.tif", road_zones_arr)
os.system(f"rm -Rf {TEMP_FOLDER}MarineRealms")
except pyogrio.errors.DataSourceError as e:
print(
"> Download ocean biomes: https://auckland.figshare.com/articles/"
+ "dataset/GIS_shape_files_of_realm_maps/5596840?file=9737926"
)
print("\n\n>> Preparing ocean name data from ocean dataset")
try:
os.system(f"unzip -o {TEMP_FOLDER}iho -d {TEMP_FOLDER}iho")
dat = gpd.read_file(f"{TEMP_FOLDER}iho/iho.shp")
shapes = dat[["geometry", "mrgid"]].values.tolist()
road_zones_arr = rasterio.features.rasterize(
shapes,
fill=-1,
out_shape=out_shape,
transform=rasterio.transform.from_bounds(
-180, -90, 180, 90, out_shape[1], out_shape[0]
),
)
imwrite(TEMP_FOLDER + "dat.tif", road_zones_arr)
pd.DataFrame(dat[["name", "mrgid"]]).to_csv(
TEMP_FOLDER + "ocean_names_to_IDs.csv", index=False
)
os.system(f"rm -Rf {TEMP_FOLDER}iho")
except pyogrio.errors.DataSourceError as e:
print(
"> Download ocean borders: http://geo.vliz.be:80/geoserver/wfs?request="
+ "getfeature&service=wfs&version=1.0.0&typename=MarineRegions:"
+ "iho&outputformat=SHAPE-ZIP"
)
print("\n\n>> Preparing country data from Natural Earth dataset")
try:
os.system(
f"unzip -o {TEMP_FOLDER}ne_10m_admin_0_countries_lakes -d {TEMP_FOLDER}NE_countries"
)
dat = gpd.read_file(f"{TEMP_FOLDER}NE_countries/ne_10m_admin_0_countries_lakes.shp")
shapes = dat[["geometry", "NE_ID"]].values.tolist()
road_zones_arr = rasterio.features.rasterize(
shapes,
fill=-1,
out_shape=out_shape,
transform=rasterio.transform.from_bounds(
-180, -90, 180, 90, out_shape[1], out_shape[0]
),
)
imwrite(TEMP_FOLDER + "countries.tif", road_zones_arr)
pd.DataFrame(dat[["NAME", "NE_ID"]]).to_csv(
TEMP_FOLDER + "country_names_to_IDs.csv", index=False
)
os.system(f"rm -Rf {TEMP_FOLDER}NE_countries")
except pyogrio.errors.DataSourceError as e:
print(
"> Download country borders: https://www.naturalearthdata.com/http//www."
+ "naturalearthdata.com/download/10m/cultural/ne_10m_admin_0_countries.zip"
)
print("\n\n>> Preparing vegetation data from WWF biome dataset")
try:
os.system(f"unzip -o {TEMP_FOLDER}6kcchn7e3u_official_teow.zip -d {TEMP_FOLDER}biomes_wwf")
dat = gpd.read_file(TEMP_FOLDER + "biomes_wwf/official/wwf_terr_ecos.shp")
shapes = dat[["geometry", "BIOME"]].values.tolist()
road_zones_arr = rasterio.features.rasterize(
shapes,
fill=-1,
out_shape=out_shape,
transform=rasterio.transform.from_bounds(
-180, -90, 180, 90, out_shape[1], out_shape[0]
),
)
imwrite(TEMP_FOLDER + "BIOMES.tif", road_zones_arr)
shapes = dat[["geometry", "ECO_ID"]].values.tolist()
road_zones_arr = rasterio.features.rasterize(
shapes,
fill=-1,
out_shape=out_shape,
transform=rasterio.transform.from_bounds(
-180, -90, 180, 90, out_shape[1], out_shape[0]
),
)
imwrite(TEMP_FOLDER + "ECOREGIONS.tif", road_zones_arr)
pd.DataFrame(dat[["ECO_ID", "ECO_NAME", "REALM", "BIOME"]]).to_csv(
TEMP_FOLDER + "ecoregions.csv", index=False
)
os.system(f"rm -Rf {TEMP_FOLDER}biomes_wwf")
except pyogrio.errors.DataSourceError as e:
print(
"> Download terrestrial biomes: https://files.worldwildlife.org/"
+ "wwfcmsprod/files/Publication/file/6kcchn7e3u_official_teow.zip"
)
##-;
""" Accepted pylint errors *************
133:0: W0105: String statement has no effect (pointless-string-statement)
9:0: W0611: Unused features imported from rasterio (unused-import)
"""