Skip to content

Instantly share code, notes, and snippets.

@futuraprime
Created January 13, 2014 16:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futuraprime/8403749 to your computer and use it in GitHub Desktop.
Save futuraprime/8403749 to your computer and use it in GitHub Desktop.
import re
import json
import numpy as np
from BeautifulSoup import BeautifulStoneSoup as Stone
f = open('new-basemap-borders-01.svg')
svg = f.read()
f.close()
soup = Stone(svg)
polys = soup.findAll('polyline')
listing = []
t = [15.7475,0.0,0.0,15.7475,98.351,147.526] # the transform params
#t = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
mat = np.matrix([
[ t[0], t[2], t[4] ],
[ t[1], t[3], t[5] ],
[ 0.0, 0.0, 1.0 ]
])
for poly in polys:
string = poly['points'].strip()
string = re.sub( ' ?[\r\n]{1,2}\t+', ' ', string )
# print string
points = string.split(' ')
for i, point in enumerate(points):
p = map( lambda x: [ float(x) ], point.split(',') )
p.append( [ 1.0 ] )
pm = np.matrix( p )
xform = mat * pm
formd = map(lambda x: str( round( x, 3 ) ), xform.transpose().tolist()[0][0:2])
points[i] = ','.join(formd)
string = ' '.join(points)
listing.append( string )
print json.dumps( listing )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment