-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
importlayers.py
171 lines (144 loc) · 6.54 KB
/
importlayers.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#########################################################################
#
# Copyright (C) 2021 OSGeo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
import os
import argparse
import datetime
import requests
from urllib.parse import urljoin
from io import BufferedReader, IOBase
from requests.auth import HTTPBasicAuth
from django.utils import timezone
from django.core.management.base import BaseCommand
parser = argparse.ArgumentParser()
class Command(BaseCommand):
help = (
"Brings a data file or a directory full of data files into a"
" GeoNode site. Layers are added to the Django database, the"
" GeoServer configuration, and the pycsw metadata index."
)
def add_arguments(self, parser):
# Positional arguments
parser.add_argument("path", nargs="*", help="path [path...]")
parser.add_argument("-hh", "--host", dest="host", help="Geonode host url")
parser.add_argument("-u", "--username", dest="username", help="Geonode username")
parser.add_argument("-p", "--password", dest="password", help="Geonode password")
def handle(self, *args, **options):
if not len(options["path"]) > 0:
self.print_help('manage.py', 'importlayers')
return
host = options.get("host") or "http://localhost:8000"
username = options.get("username") or "admin"
password = options.get("password") or "admin"
start = datetime.datetime.now(timezone.get_current_timezone())
for path in options["path"]:
success, errors = GeoNodeUploader(
host=host, username=username, password=password, folder_path=path
).execute()
finish = datetime.datetime.now(timezone.get_current_timezone())
td = finish - start
duration = td.microseconds / 1000000 + td.seconds + td.days * 24 * 3600
print(f"{(duration * 1.0 / len(os.listdir(options['path'][0])))} seconds per layer")
output = {"success": success, "errors": errors}
print(f"Output data: {output}")
class GeoNodeUploader:
def __init__(
self,
host: str,
folder_path: str,
username: str,
password: str,
call_delay: int = 10,
**kwargs,
):
self.host = host
self.folder_path = folder_path
self.username = username
self.password = password
self.call_delay = call_delay
def execute(self):
success = []
errors = []
for root, subdirs, files in os.walk(self.folder_path):
for file in files:
_file = os.path.join(root, file)
print(f"Scanning: {_file}")
if not os.path.exists(_file):
print(f"The selected file path does not exist: {_file}")
continue
spatial_files = ("dbf_file", "shx_file", "prj_file")
base, ext = os.path.splitext(_file)
params = {
# make public since wms client doesn't do authentication
"permissions": '{ "users": {"AnonymousUser": ["view_resourcebase"]} , "groups":{}}', # to be decided
"time": "false",
"dataset_title": file,
"charset": "UTF-8",
}
if ext.lower() == ".shp":
for spatial_file in spatial_files:
ext, _ = spatial_file.split("_")
file_path = f"{base}.{ext}"
# sometimes a shapefile is missing an extra file,
# allow for that
if os.path.exists(file_path):
params[spatial_file] = open(file_path, "rb")
elif ext.lower() == ".tif":
file_path = base + ext
params["tif_file"] = open(file_path, "rb")
else:
continue
files = {}
client = requests.session()
with open(_file, "rb") as base_file:
params["base_file"] = base_file
for name, value in params.items():
if isinstance(value, BufferedReader):
files[name] = (os.path.basename(value.name), value)
params[name] = os.path.basename(value.name)
response = client.post(
urljoin(self.host, "/api/v2/uploads/upload/"),
auth=HTTPBasicAuth(self.username, self.password),
data=params,
files=files,
)
print(f"{file}: {response.status_code}")
if isinstance(params.get("tif_file"), IOBase):
params["tif_file"].close()
data = response.json()
if data['status'] == 'finished':
if data['success']:
success.append(file)
else:
errors.append(file)
elif 'redirect_to' in data:
import_id = int(data["redirect_to"].split("?id=")[1].split("&")[0])
upload_response = client.get(f"{self.host}/api/v2/uploads/")
upload_id = self._get_upload_id(upload_response, import_id)
client.get(f"{self.host}/api/v2/uploads/{upload_id}")
client.get(f"{self.host}/upload/check?id={import_id}")
client.get(f"{self.host}/upload/final?id={import_id}")
success.append(file)
else:
errors.append(file)
return success, errors
@staticmethod
def _get_upload_id(upload_response, import_id):
for item in upload_response.json()["uploads"]:
if item.get("import_id", None) == import_id:
return item.get("id", None)