-
Notifications
You must be signed in to change notification settings - Fork 40
/
genCountriesGeoJSON.py
executable file
·118 lines (98 loc) · 3.38 KB
/
genCountriesGeoJSON.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
#!/usr/bin/python
#import requests
import json
import os
import sys
import getopt
import string
featureKey="ADM0_A3"
inputFile="countries.geojson"
outputPath = "geojson"
adminNameKey=None
willForceUpdate=0
geojsonLinks={}
#shouldLint = 1
#validate_endpoint = 'http://geojsonlint.com/validate'
#if no parameters are supplied, go with defaults
#otherwise parse the command line for the options
if len(sys.argv) > 1:
#specify valid options
shortOpts = "k:i:o:g:f"
longOpts = [ "inputFile=", "outputPath=","keyName=","force", "genReadme="]
try:
opts, args = getopt.getopt(sys.argv[1:], shortOpts, longOpts)
for opt,arg in opts:
if opt in ("-k", "--keyName"):
featureKey=arg
elif opt in ("-i", "--inputFile"):
inputFile=arg
elif opt in ("-o", "--outputPath"):
outputPath=arg
elif opt in ("-g", "--genReadme"):
adminNameKey=arg
elif opt in ("-f", "--force"):
willForceUpdate = 1
except getopt.GetoptError as e:
print """Invalid syntax!
Usage: -k | --keyName <attribute key name in geojson file>
-i | --inputFile <input geojson file>
-o | --outputPath <output path>
-g | --genReadme <attribute key for country name in geojson file>
-f if supplied, existing files will be overwritten
"""
sys.exit(-1)
try:
print "Reading geojson file: %s ..." % inputFile,
sys.stdout.flush()
fp = open(inputFile)
geoJSONList = fp.readlines()
fp.close
print "DONE"
except IOError:
print "Could not open geojson file for reading:" + inputFile
sys.exit(-1)
print "Parsing ...",
sys.stdout.flush()
geoJSONData = json.loads(' '.join(geoJSONList))
print "DONE"
geoFeatures = geoJSONData["features"]
if adminNameKey:
readmePath = outputPath + os.sep + "README.md"
try:
fpAdmin = open(readmePath, "w")
fpAdmin.write("# GeoJSON Countries list\n\n")
except IOError:
print "Could not open %s for writing!" % readmePath
adminNameKey = None
for curFeature in geoFeatures:
curProperties = curFeature["properties"]
curKey = curProperties[featureKey]
print "Creating geojson file for " + curKey
#if and admin key is defined, write out current country to readme file
if adminNameKey:
curAdminVal = curProperties[adminNameKey]
if (curAdminVal):
linkStr = u"- [{0}](../../../blob/master/{1}/{2}.geojson)\n".format(curAdminVal, outputPath, curKey)
geojsonLinks[curAdminVal] = linkStr
curFilename = outputPath + os.sep + curKey + ".geojson"
if os.path.exists(curFilename):
if willForceUpdate:
os.remove(curFilename)
else:
print "File already exists!"
continue
curGeoDataStr = json.dumps(curFeature)
#lint_request = requests.post(validate_endpoint, data=curGeoDataStr)
#print lint_request.json()
#save out to file
try:
fpo = open(curFilename, "w")
fpo.write(curGeoDataStr)
fpo.close()
except IOError:
print "Failed to create file:" + curFilename
if adminNameKey:
countriesList = sorted(list(geojsonLinks.keys()), key=lambda s: s.lower())
for countryKey in countriesList:
fpAdmin.write(geojsonLinks[countryKey].encode("utf-8"))
fpAdmin.close()