Permalink
Browse files

Fix pen width to QGIS size - closes #1

  • Loading branch information...
1 parent 6c84a60 commit 5830287d5fcd74494fd6632d29bde400b34e9f3e @NathanW2 committed Oct 31, 2011
Showing with 34 additions and 48 deletions.
  1. +30 −14 mapinfoToQgis.py
  2. +4 −34 unitTests.py
View
@@ -2,6 +2,7 @@
import re
import string
from PyQt4.QtCore import QChar
+from PyQt4.QtGui import QApplication,QPaintDevice, QPainter
from optparse import OptionParser
from templates import templateLookup
@@ -76,8 +77,8 @@ def generateSymbol(self,mapbasicString, name):
pass
def translateSimpleSymbol(self,mapbasicString):
- ''' Translates a Mapbasic 3.0 Symbol into a new font MapInfo Symbol
- '''
+ """ Translates a Mapbasic 3.0 Symbol into a new font MapInfo Symbol """
+
# MAPBASIC Font Symbol syntax:
# Symbol ( shape, color, size )
@@ -141,17 +142,17 @@ def generateLineFromPen(self, mapbasicString, name):
rgbString = "%s,%s,%s" % (rgb[0],rgb[1],rgb[2])
values = dict(
color = rgbString, # Color needs to be converted to RGB
- width = self.pointTomm(tokens[0]), # Mapasic size 3 points == 1 mm
+ width = self.penWidthTomm(tokens[0]),
name = name)
return lineTemplate.safe_substitute(values)
def generateFieldMap(self, fieldValueMap):
- ''' Generates qml block with symbol number, value, label mapping
+ """ Generates qml block with symbol number, value, label mapping
fieldValueMap -- A list of tuples containing number, value, label maps.
- '''
+ """
if fieldValueMap is None or len(fieldValueMap) == 0:
return None
@@ -171,7 +172,7 @@ def generateFieldMap(self, fieldValueMap):
return categories
def createQmlFromFile(self, asciiFile,outName,columnName):
- ''' Writes a qml file from a | delimited file
+ """ Writes a qml file from a | delimited file
Syntax of input file:
{value} | {label} | {font style}
or
@@ -180,7 +181,7 @@ def createQmlFromFile(self, asciiFile,outName,columnName):
asciiFile -- Name of the input file in the supported format
outName -- Name of the output qml file
columnName -- Name of the column that contains the values.
- '''
+ """
styles = open(asciiFile)
fields = []
@@ -203,8 +204,7 @@ def createQmlFromFile(self, asciiFile,outName,columnName):
print "Generating QGIS styles for:"
symbolsList = []
for symbolNo in symbols:
- print symbols[symbolNo]
- print symbolNo
+ print "No. %s : %s " % (symbolNo, symbols[symbolNo])
symbolqml = gen.generateSymbol(symbols[symbolNo],symbolNo)
if not symbolqml is None:
symbolsList.append(symbolqml)
@@ -218,13 +218,14 @@ def createQmlFromFile(self, asciiFile,outName,columnName):
outqml.close()
def createQMLFromMapInfoTable(self,mapinfoTable, outName, columnName):
- ''' Opens MapInfo and generates a qml file from the supplied table.
+ """ Opens MapInfo and generates a qml file from the supplied table.
mapinfoTable -- Name of the input tab file.
outName -- Name of the output qml file
columnName -- Name of the column that contains the values.
WARNING: Very little error handling here.
- '''
+ """
+
import tempfile, os
from win32com.client import Dispatch
openTable = 'Open Table "%s" as tempMapInfoToQGIS' % mapinfoTable
@@ -233,7 +234,7 @@ def createQMLFromMapInfoTable(self,mapinfoTable, outName, columnName):
try:
os.remove(tempoutput)
except WindowsError:
- #Do nothing here a MapInfo will create the file.
+ #Do nothing here as MapInfo will create the file.
pass
exportString = 'Export "outputTable" Into "%s" Type "ASCII" Delimiter "|" CharSet "WindowsLatin1"' % tempoutput
@@ -250,12 +251,12 @@ def createQMLFromMapInfoTable(self,mapinfoTable, outName, columnName):
self.createQmlFromFile(tempoutput,outName,columnName)
def colorToRGB(self, colorValue):
- ''' Returns a RGB tuple from a Mapbasic color value
+ """ Returns a RGB tuple from a Mapbasic color value
Formula:
R = RGB \ 65536
G = (RGB - R*65536) \ 256
B = RGB - R*65536 - G*256
- '''
+ """
color = int(colorValue)
red = color / 65536
green = (color - red * 65536) / 256
@@ -265,6 +266,21 @@ def colorToRGB(self, colorValue):
def pointTomm(self,pointSize):
return float(pointSize) / 3
+ def penWidthTomm(self, pensize):
+ # Pen width info from http://www.gissky.com/download/Download/DataFormat/Mapinfo_Mif.pdf
+ # Width is a number from 1 to 7. 1-7 is the width in screen pixels. 11-2047 are values that will be
+ # converted to points:
+ # penwidth = (number of points * 10) + 10
+ pensize = float( pensize )
+ if ( pensize >= 11 ):
+ pointsize = (float(pensize) - 10) / 10
+ return pointsize / 3
+ else:
+ # This isn't really right as pixel size depends on the moniter settings but
+ # working it out is a real pain so using this method will work for now.
+ return pensize / 3
+
+
if __name__ == '__main__':
gen = StyleGenerator()
usage = '''usage: %prog inputFile outQmlFile [options]
View
@@ -22,40 +22,10 @@ def testPoint_To_QGISSize(self):
point = 3
self.assertEqual(self.gen.pointTomm(point),expected)
-
- def testQml_Output_With_One_Symbol_Block(self):
- symbolblock = '''<symbol outputUnit="MM" alpha="1" type="marker" name="0" >
-<layer pass="0" class="FontMarker" locked="0" >
- <prop k="angle" v="0" />
- <prop k="chr" v="e" />
- <prop k="color" v="160,64,255" />
- <prop k="font" v="MapInfo Cartographic" />
- <prop k="offset" v="0,0" />
- <prop k="size" v="3.0" />
-</layer>
-</symbol>'''
-
- expected = '''<qgis>
-<renderer-v2 symbollevels="0" type="singleSymbol">
-<symbols>
-<symbol outputUnit="MM" alpha="1" type="marker" name="0" >
-<layer pass="0" class="FontMarker" locked="0" >
- <prop k="angle" v="0" />
- <prop k="chr" v="e" />
- <prop k="color" v="160,64,255" />
- <prop k="font" v="MapInfo Cartographic" />
- <prop k="offset" v="0,0" />
- <prop k="size" v="3.0" />
-</layer>
-</symbol>
-</symbols>
-</renderer-v2>
-</qgis>'''
- output = self.gen.generateQml([symbolblock],None,None)
- print expected
- print output
- print ''.join(difflib.unified_diff(expected.splitlines(1),output.splitlines(1)))
- self.assertEqual(output,expected)
+ def testPenWidthToPoints(self):
+ expected = 3
+ penwdith = 40
+ self.assertEqual(self.gen.penWidthToPoint(penwdith),expected)
def testFont_Symbol_Generation(self):
mapbasic = 'Symbol (101,10502399,9,"MapInfo Cartographic",1,0)'

0 comments on commit 5830287

Please sign in to comment.