-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshp2kml.py
More file actions
40 lines (29 loc) · 980 Bytes
/
shp2kml.py
File metadata and controls
40 lines (29 loc) · 980 Bytes
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
from lxml import etree
from pykml.factory import KML_ElementMaker as KML
import shapefile
import sys
def create_reader(args):
shp_fname = args[1]
dbf_fname = args[2]
shp_file = open(shp_fname, 'rb')
dbf_file = open(dbf_fname, 'rb')
reader = shapefile.Reader(shp=shp_file, dbf=dbf_file)
return reader
def create_kml_root():
doc = KML.kml()
return etree.SubElement(doc, 'Document')
def main(args):
reader = create_reader(args)
doc = create_kml_root()
for shapeRec in reader.shapeRecords():
name = shapeRec.record[1]
points = shapeRec.shape.points
doc.append(KML.Placemark(
KML.name(name),
KML.LineString(
KML.coordinates(
' '.join([str(item).strip('[]').replace(' ', '') for item in points])))))
print(etree.tostring(doc))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))