-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadMartikkel.py
170 lines (152 loc) · 4.78 KB
/
loadMartikkel.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
"""
Program to get properties border coordinates from Geonorge using their api or a json file downloaded Geonorge
Writes the data as WKT data for us in QGIS or other GIS software
"""
import sys
import json
import getopt
import requests
def usage(script):
"""
How to use the program
"""
print(script, '-f jasonfile .... to load matrikkel coordinates from file')
print('or')
print(script, '-k kommunenr -g gardsnummer -b bruksnummer -e EPGS code .... to load from GeoNorge using their api')
sys.exit(2)
def parseArguments(name, argv):
"""
Parse the program arguments. See function usage.
"""
kommunenr = 0
gnr = 0
bnr = 0
epgs = 0
file_name = ""
if len(argv) == 2:
try:
opts, arg = getopt.getopt(argv,"f:")
except getopt.GetoptError as err:
print(err)
usage(name)
if len(argv) == 8:
try:
opts, arg = getopt.getopt(argv,"k:g:b:e:")
except getopt.GetoptError as err:
print(err)
usage(name)
for opt, arg in opts:
if opt == '-k':
try:
kommunenr = int(arg)
except ValueError:
usage(name)
if opt == '-g':
try:
gnr = int(arg)
except ValueError:
usage(name)
if opt == '-b':
try:
bnr = int(arg)
except ValueError:
usage(name)
if opt == '-e':
try:
epgs = int(arg)
except ValueError:
usage(name)
if opt == '-f':
file_name = arg
return(kommunenr, gnr, bnr, epgs, file_name)
def getfromGeoNorge(kommunenr, gnr, bnr, epgs):
"""
Get propery border coordinates from Geonorge using their api
"""
url = "https://ws.geonorge.no/eiendom/v1/geokoding?omrade=true&kommunenummer=" + str(kommunenr) + "&gardsnummer=" + str(gnr) + "&bruksnummer=" + str(bnr) + "&utkoordsys=" + str(epgs)
response = requests.get(url, timeout=60)
if response.status_code == 200:
return response.json()
print("Error getting data from GeoNorge. Reurn code = ", response.status_code)
sys.exit(3)
def readJsonFile(file_name):
"""
Read a json file downloaded from Geonorge
"""
try:
with open(file_name, 'r', encoding="utf-8") as file:
data = json.load(file)
return data
except (OSError, json.decoder.JSONDecodeError) as err:
print("File", file_name, "does not exsist, is not readable or not a json file")
print("Error reported:", err)
sys.exit(4)
def writePolygon(coord):
"""
Write WKT datatype POLYGON
"""
print("POLYGON ((", end="", sep="")
formatPoints(coord, "", "")
print("))")
def writeLine(coord):
"""
Write WKT datatype LINESTRING
"""
print("LINESTRING (", end="", sep="")
formatPoints(coord, "", "")
print(")")
def writePoints(coord):
"""
Write WKT datatype MULTIPOINT
"""
print("MULTIPOINT (", end="", sep="")
formatPoints(coord, "(", ")")
print(")")
def formatPoints(coord, c1, c2):
"""
Write the coordinates for the points in the WKT datatypes
"""
n = len(coord[0])
i = 1
for coo in coord[0]:
if i < n:
print(c1, coo[0], " ", coo[1], c2 , ", ", end="", sep="")
else:
print(c1, coo[0], " ", coo[1], c2, end="", sep="")
i = i + 1
def main(script, argv):
"""
Parse arguments. Get data from Geonorge or exsisting downloaded json file
Try to get the coordinates for up to 100 polygon in th einput data
Wites the data as WKT
"""
if len(argv) != 8 and len(argv) != 2:
usage(script)
kommunenr, gnr, bnr, epgs, file_name = parseArguments(script, argv)
if kommunenr > 0 and gnr > 0 and bnr > 0 and epgs > 0:
data = getfromGeoNorge(kommunenr, gnr, bnr, epgs)
elif file_name != "" :
data = readJsonFile(file_name)
else:
print("Fatal error in argument parsing. Exiting ....")
sys.exit(5)
geometry = data['features']
n = 0
while n < 100:
try:
ltype = geometry[n]['geometry']
if ltype['type'] == 'Polygon':
coord = ltype['coordinates']
writePolygon(coord)
writePoints(coord)
writeLine(coord)
print()
n = n + 1
except IndexError:
break
if n == 0:
print("No Polygon data found")
sys.exit(7)
if __name__ == "__main__":
scriptName = sys.argv[0]
main(scriptName, sys.argv[1:])