Skip to content

Instantly share code, notes, and snippets.

@christippett
Created July 19, 2018 12:15
Show Gist options
  • Save christippett/1f9a68eb8690dd86f08c10b960f7e1f0 to your computer and use it in GitHub Desktop.
Save christippett/1f9a68eb8690dd86f08c10b960f7e1f0 to your computer and use it in GitHub Desktop.
Convert online shopping centre maps (in SVG) to well-known text (WKT)
"""
Created on Sun Sep 07 13:11:14 2014
@author: Chris
"""
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
import csv
import re
# svg namespace
svgNS = 'http://www.w3.org/2000/svg'
# load svg file
svgFile = open('Stockland_Merrylands.svg','r')
# load csv file
csvFile = open('svg_geometries.csv','wb')
csvWriter = csv.writer(csvFile, delimiter=',')
tree = ET.ElementTree()
tree.parse(svgFile)
geomWKT = []
for rect in tree.findall('.//{%s}rect' % svgNS):
x = float(rect.attrib['x'])
y = float(rect.attrib['y'])
height = float(rect.attrib['height'])
width = float(rect.attrib['width'])
points = []
points.append('{0} {1}'.format(x, y*-1))
points.append('{0} {1}'.format(x+width, y*-1))
points.append('{0} {1}'.format(x+width, (y+height)*-1))
points.append('{0} {1}'.format(x, (y+height)*-1))
points.append('{0} {1}'.format(x, y*-1))
points = filter(None, points)
wkt = ', '.join(points)
wkt = 'POLYGON(('+wkt+'))'
geomWKT.append(wkt)
for polygon in tree.findall('.//{%s}polygon' % svgNS):
points = polygon.attrib['points']
points = points.split(' ')
points = [p.replace(',',' -') for p in points]
points.append(points[0])
points = filter(None, points)
wkt = ', '.join(points)
wkt = 'POLYGON(('+wkt+'))'
geomWKT.append(wkt)
for polyline in tree.findall('.//{%s}polyline' % svgNS):
points = polyline.attrib['points']
points = points.split(' ')
points = [p.replace(',',' -') for p in points]
points.append(points[0])
points = filter(None, points)
wkt = ', '.join(points)
wkt = 'POLYGON(('+wkt+'))'
geomWKT.append(wkt)
for path in tree.findall('.//{%s}path' % svgNS):
pattern = re.compile(r'(([A-Z|a-z])((?:-?[0-9]+(?:\.[0-9]*)?)(?:(?:,|-)(?:-?[0-9]+(?:\.[0-9]*)?))*))')
pattern2 = re.compile(r'((-?[0-9]+\.?[0-9]*)(?:,)?((?:-?[0-9]+\.?[0-9]*))?)')
pathPoints = path.attrib['d']
points = []
for pathElem in re.findall(pattern, pathPoints):
pathCoords = re.findall(pattern2, pathElem[2])
tempx = pathCoords[-1][1]
if tempx != '': tempx = float(tempx)
tempy = pathCoords[-1][2]
if tempy != '': tempy = float(tempy)
if pathElem[1] == 'M':
x = tempx
y = tempy * -1
elif pathElem[1] == 'l':
x = x + tempx
y = y + (tempy * -1)
elif pathElem[1] == 'L':
x = tempx
y = tempy * -1
elif pathElem[1] == 'h':
x = x + tempx
y = y
elif pathElem[1] == 'H':
x = tempx
y = y
elif pathElem[1] == 'v':
tempy = tempx
x = x
y = y + (tempy * -1)
elif pathElem[1] == 'V':
tempy = tempx
x = x
y = tempy * -1
elif pathElem[1] == 'c':
x = x + tempx
y = y + (tempy * -1)
elif pathElem[1] == 'C':
x = tempx
y = tempy * -1
tempPoint = str(x) + ' ' + str(y)
points.append(tempPoint)
points.append(points[0])
points = filter(None, points)
wkt = ', '.join(points)
wkt = 'POLYGON(('+wkt+'))'
geomWKT.append(wkt)
for i, value in enumerate(geomWKT):
csvWriter.writerow([i, value])
csvFile.close()
svgFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment