-
Notifications
You must be signed in to change notification settings - Fork 2
/
GJWriter.py
106 lines (99 loc) · 3.64 KB
/
GJWriter.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
"""
Writing a GeoJSON File.
Note : Depended Fiona(Python Extend Module) data model.
"""
class GJWriter:
"""
A class for writing GeoJSON file.
"""
def __init__( self ,inOutFile ):
"""
Constructor method.
:param inOutFile: Name of the file to output
:type inOutFile: String
"""
self._jsonfd = open(inOutFile ,'w' ,encoding='utf-8')
# Clear properties.
self.Geometry = None
self.Properties = None
self.Tippecanoe = None
def __del__(self):
"""
Destructor method.
"""
# Output Footer and close file.
self._jsonfd.write( "\n" + "]}")
self._jsonfd.close()
def setGeometry(self ,inGeom):
"""
Set "Geometry" member.
:param inGeom: Geometry data to output
:type inGeom: Objects in Fiona Geometry
"""
self.Geometry = str(inGeom).replace("(","[").replace(")","]").replace("\'","\"")
def setProperty(self ,inName ,inValue):
"""
Set "Property" member.
"Tippecanoe" is special member for the tippecanoe utility.
:param inName: Name of the property
:type inName: String
:param inValue: Value of the property
:type inValue: String or Integer
"""
# Tabs for the first item, commas for the second and subsequent items
if self.Properties==None:
self.Properties= ""
else:
self.Properties+= ","
# Set value
if type(inValue) is int:
self.Properties+= "\"" + inName + "\"" + ":" + str(inValue)
else:
self.Properties+= "\"" + inName + "\"" + ":" + "\"" + str(inValue) + "\""
def setTippecanoe(self ,inName ,inValue):
"""
Set "Tippecanoe" member.
"Tippecanoe" is an attribute of Feature object.
:param inName: Name of the tippecanoe
:type inName: String
:param inValue: Value of the tippecanoe
:type inValue: String or Integer
"""
# Tabs for the first item, commas for the second and subsequent items
if self.Tippecanoe==None:
self.Tippecanoe= ""
else:
self.Tippecanoe += ","
# Set value
if type(inValue) is int:
self.Tippecanoe += "\"" + inName + "\"" + ":" + str(inValue)
else:
self.Tippecanoe += "\"" + inName + "\"" + ":" + "\"" + str(inValue) + "\""
def Write(self):
"""
Writing GeoJSON file.
"""
# Header for the first Feature, commas and LF for the second and subsequent items
if self._jsonfd.tell()==0 :
self._jsonfd.write("{" + "\"" + "type" + "\"" + ":" + "\"" + "FeatureCollection" + "\"" + "," + "\"" + "features" + "\"" + ": [" + "\n")
else:
self._jsonfd.write(",\n")
# Set "type" member(Header for the Feature)
self._jsonfd.write("\t{\"type\":\"Feature\",")
# Set "Geometry" member
self._jsonfd.write("\"geometry\":" + self.Geometry + ",")
# Set "Tippecanoe" member
if self.Tippecanoe != None:
self._jsonfd.write("\"tippecanoe\":{" + self.Tippecanoe + "},")
# Set "Property" member
if self.Properties != None:
self._jsonfd.write("\"properties\":{" + self.Properties + "}")
# End of Feature
self._jsonfd.write("}")
# Clear all properties
self.Clear()
def Clear(self):
# Clear all properties
self.Geometry = None
self.Properties = None
self.Tippecanoe = None