-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2geojson_polygon_stbrd_pt.py
More file actions
89 lines (67 loc) · 2.46 KB
/
Copy pathjson2geojson_polygon_stbrd_pt.py
File metadata and controls
89 lines (67 loc) · 2.46 KB
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
import json
def latitude_dms_to_decimal(coordinate):
degrees = int(coordinate[:2])
minutes = int(coordinate[2:4])
seconds = float(coordinate[4:-1])
direction = coordinate[-1]
decimal = degrees + (minutes / 60) + (seconds / 3600)
if direction in ['S', 'W']:
decimal = -decimal
return decimal
def longitude_dms_to_decimal(coordinate):
degrees = int(coordinate[:3])
minutes = int(coordinate[3:5])
seconds = float(coordinate[5:-1])
direction = coordinate[-1]
decimal = degrees + (minutes / 60) + (seconds / 3600)
if direction in ['S', 'W']:
decimal = -decimal
return decimal
def convert_json_to_geojson(json_data):
features = []
for item in json_data:
if "Data" in item:
for obj in item["Data"]["Object"]:
# lon = 0
# lat = 0
properties = {
"Identification": obj["Identification"],
"Name": obj["Name"],
"Lower_limit": obj["Lower_limit"],
"Upper_limit": obj["Upper_limit"]
}
if "State_border_point" in obj:
print(properties)
for detail in obj['State_border_point']:
lat = latitude_dms_to_decimal(detail["State_Border_point_Geo_lat"])
lon = longitude_dms_to_decimal(detail["State_Border_point_Geo_long"])
print(str(lat) + " - " + str(lon))
geometry = {
"type": "Point",
"coordinates": [lon, lat]
}
feature = {
"type": "Feature",
"geometry": geometry,
"properties": properties
}
features.append(feature)
geojson_data = {
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": features
}
return geojson_data
# Read JSON file
with open('input.json') as json_file:
json_data = json.load(json_file)
# Convert JSON to GeoJSON
geojson_data = convert_json_to_geojson(json_data)
# Write GeoJSON to file
with open('output.geojson', 'w') as geojson_file:
json.dump(geojson_data, geojson_file, indent=4)