diff --git a/src/idTagDetector.py b/src/idTagDetector.py
index 111504a..26a4f7e 100644
--- a/src/idTagDetector.py
+++ b/src/idTagDetector.py
@@ -1,107 +1,126 @@
# takes a string as input. Detects SVG tags susceptible of designating geographical divisions
# such as continents, countries, administrative subdivitions, etc.
#from bs4 import BeautifulSoup
import re
+# Aligns tags with CSS conventions for valid names/selectors
+def alignCssSelectors(argString):
+ #argString = re.sub("[~!@$%^&*()+=,./';:\"?><[\]\{}|`# ]","_",argString) # replace invalid characters with underscores. No idea why that does not work
+ argString = re.sub(",","_",argString) # replace commas with underscores
+ argString = re.sub(" ","_",argString) # replace spaces with underscores
+ return argString
+# Detects and handles identifications.
class IdTagDetector:
def __init__(self, arg):
self.arg=arg
self.detectedTags=[]
- self.stoplist=[]
+ #self.stoplist=["path", "svg", "g", "clipPath", "rect", "stop", "style", "metadata", "title", "defs", "linearGradient", "#State_borders", "separator"]
+ self.stoplist=["path", "svg", "g", "clipPath", "rect", "stop", "style", "metadata", "title", "defs", "linearGradient"]
self.stoplistPath="../configs/stoplists"
self.whitelist=[]
self.whitelistPath="../configs/whitelists"
# The prefixes that have been observed to hold geographic labels in the test maps
#self.labelPrefixes = ["path class", "label", "label", "id"]
- self.labelPrefixes = ["path class", "inkscape:label", "label", "id"]
+ #self.labelPrefixes = ["path", "class", "inkscape:label", "label", "id"]
+ self.labelPrefixes = ["inkscape:label", "label", "id"]
self.regex = "\s*=\s*\"([^0-9].+?)\""
# TODO: implements this
def loadStopList(self):
return []
+ #returns a regex formed of all words in stoplist. These will never be used as semantically informative geographical IDs.
+ def stopListRegex(self):
+ regex = "("
+ for i in self.stoplist:
+ regex = regex+i+"|"
+ regex = regex+" )\d*"
+ #return re.compile(regex)
+ return regex
+
+
# TODO: implements this
def loadWhiteList(self):
return []
-
# Just to print what has been found, fairly trivial
def listTags(self):
print(self.detectedTags)
# Seeks prefixes as listed in "self.labelPrefixes", and retrieves following srings if not made entirely of numbers.
def detect(self):
candidates = set()
for labelPrefix in self.labelPrefixes:
for i in ( re.findall(labelPrefix+self.regex, self.arg) ):
- if not re.match("(path|svg|g|clipPath|rect|stop|style|metadata|title|defs|linearGradient)\d+", i):
+ #if not re.match("(path|svg|g|clipPath|rect|stop|style|metadata|title|defs|linearGradient)\d+", i):
+ if not re.match(self.stopListRegex(), i):
candidates.add(i)
self.detectedTags = candidates
# Return a list of prefixes most likely used in this SVG document to store IDs.
def detectedPrefixes(self):
usedPrefixes = []
for labelPrefix in self.labelPrefixes:
if len( re.findall(labelPrefix+self.regex, self.arg) ) > 0:
usedPrefixes.append(labelPrefix)
return usedPrefixes
# Just to return what has been found, fairly trivial
#def detectedTags(self):
#return self.detectedTags
#subdivisionTag = "path"
#labelTag = "inkscape:label"
#soup = BeautifulSoup(self.arg)
#candidates = soup.findAll(subdivisionTag)
#for candidate in candidates:
#print(candidate)
##if candidate[labelTag][0] == "#":
##break
##if candidate.has_attr(labelTag):
##self.detectedTags.append(candidate[labelTag])
##self.detectedTags.append(candidate[labelTag])
#####################################################################
#####################################################################
###############################################
#../maps/Blank_Map_Africa_1932.svg
#
# ==> tn
###############################################
#../maps/USA_Counties_with_FIPS_and_names.svg
#
# ==> Teton, WY
###############################################
#../maps/Blank_map_of_Europe_1815.svg
#
# ==> Serbia
###############################################
#../maps/World98.svg
#
#
#
#==> Iran:Semnan Province
#####################################################################
# path class="land tn"
# inkscape:label="Teton, WY"
# id="Serbia"
#
diff --git a/src/svgColourManager.py b/src/svgColourManager.py
index 4d1b6bd..64d31da 100644
--- a/src/svgColourManager.py
+++ b/src/svgColourManager.py
@@ -1,71 +1,87 @@
# takes an SVG file as input. Creates a table associating geographic identifiers and their colours.
# Writes an SVG file with new colours.
#from bs4 import BeautifulSoup
#import re
class SvgColourManager:
# Just used in constructor to fill self.listOfIDs
def fillListOfIDs(self):
from idTagDetector import IdTagDetector
detector = IdTagDetector(self.inputSVG)
detector.detect()
return detector.detectedTags
def __init__(self, arg):
#self.inputSVG = open(arg, 'r').read()
self.inputSVG = arg
self.tagsAndColours = {}
self.listOfIDs = self.fillListOfIDs()
# sets all subdivision in white
def colourAllWhite(self):
for area in self.listOfIDs:
self.tagsAndColours[area] = "#FFFFFF"
# I see a red door and I want to paint it blaaaack.
# Sorry.
# sets all subdivision in black
def colourAllBlack(self):
for area in self.listOfIDs:
self.tagsAndColours[area] = "#000000"
# sets all subdivision in white
def colourAllRandom(self):
import random
for area in self.listOfIDs:
- self.tagsAndColours[area] = hex(random.randint(0, 16777215))[2:].upper()
-
+ self.tagsAndColours[area] = "#"+hex(random.randint(0, 16777215))[2:].upper()
+
+ # sets all subdivision in white
+ def colourAllRandomRed(self):
+ import random
+ for area in self.listOfIDs:
+ #self.tagsAndColours[area] = hex(random.randint(0, 16777215))[2:].upper()
+ redValue = random.randint(100, 255)
+ rgbTuple = (255, 255-redValue, 255-redValue)
+ hexColour = '%02x%02x%02x' % rgbTuple
+ self.tagsAndColours[area] = "#"+hexColour
# Outputs content of self.tagsAndColours to standard output.
def listTagsAndColours(self):
for tag in self.tagsAndColours.keys():
print(tag+"\t\t"+self.tagsAndColours[tag])
+ # blanks (sets "fill=None") the elements in the list. Useful for borders etc.
+ def blankElementsInList(self, arg):
+ for i in arg:
+ if i in self.tagsAndColours.keys():
+ self.tagsAndColours[i] = "none"
+ # Inserts CSS style instructions between header and body of the SVG file.
def editMap(self):
import re
- regex = re.compile("(<\?xml.*?>\s*\n*\s*)", re.DOTALL)
-
- print( (re.split (regex, self.inputSVG))[1] )
- print()
- # ----------------------------------------------
- print("")
- # ----------------------------------------------
- print()
- print( (re.split (regex, self.inputSVG))[2] )
-
+ outputString = outputString+"\t#"+tag+" { fill: "+self.tagsAndColours[tag]+" }\n"
+ outputString = outputString+"]]>\n"+m.group(2)
+
+ print(outputString)
+
+
+
\ No newline at end of file
diff --git a/src/testSvgColourManager.py b/src/testSvgColourManager.py
index b3072bc..4b2c383 100755
--- a/src/testSvgColourManager.py
+++ b/src/testSvgColourManager.py
@@ -1,27 +1,31 @@
#!/usr/bin/python
from svgColourManager import SvgColourManager
#-----------------------------------
# Management of arguments
#-----------------------------------
import argparse
parser = argparse.ArgumentParser(description='test of svgColourManager: takes SVG maps as argument; sould return a list of ID tags and matching colours in standard output.')
# positional argument
parser.add_argument("infiles", nargs='+', help="filenames of the SVG files against which to test svgColourManager.")
args = parser.parse_args()
##-----------------------------------
## Main
##-----------------------------------
for infile in args.infiles:
#print("Reading from "+infile+".")
svg = open(infile, 'r').read()
colourManager = SvgColourManager(svg)
+ #colourManager.colourAllRandomRed()
colourManager.colourAllRandom()
+ #colourManager.colourAllBlack()
#colourManager.listTagsAndColours()
+ colourManager.blankElementsInList(["State_border", "separator", "State_borders"])
+
colourManager.editMap()
diff --git a/src/testXSLT.py b/src/testXSLT.py
new file mode 100755
index 0000000..c07e834
--- /dev/null
+++ b/src/testXSLT.py
@@ -0,0 +1,35 @@
+#!/usr/bin/python
+
+import lxml.etree as ET
+
+
+#-----------------------------------
+# Management of arguments
+#-----------------------------------
+import argparse
+parser = argparse.ArgumentParser(description='test of XSLT')
+# positional argument
+parser.add_argument("infiles", nargs='+', help="filenames ")
+
+args = parser.parse_args()
+
+
+##-----------------------------------
+## Main
+##-----------------------------------
+for infile in args.infiles:
+ #print("Reading from "+infile+".")
+ #svg = open(infile, 'r').read()
+ svg=infile
+
+ dom = ET.parse(svg)
+ xslt = ET.parse("transformation.xslt")
+ transform = ET.XSLT(xslt)
+ newdom = transform(dom)
+ output = ET.tostring(newdom, pretty_print=True)
+
+ #print("Saving results to "+outfile+"..."),
+ outfile = "outgago.svg"
+ f = open(outfile,'w')
+ f.write(output.decode("utf-8")) # python will convert \n to os.linesep
+ f.close() # you can omit in most cases as the destructor will call if
\ No newline at end of file
diff --git a/src/transformation.xslt b/src/transformation.xslt
new file mode 100644
index 0000000..d0bb357
--- /dev/null
+++ b/src/transformation.xslt
@@ -0,0 +1,55 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+~!@$%^*()+=,./';:"?[]{}|`# & > <
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file