Skip to content

Commit

Permalink
OSM plugin & provider update from Lukas Berka:
Browse files Browse the repository at this point in the history
- new OSM style files !! map looks pretty good now... (finally)
- new icons !
- some texts on dialogs were changed and completed
- "Saving OSM into file" functionality was improved a little...
- now OSM Plugin verify if osm provider is available...
- fixed some problems with encoding... ascii --> utf-8
- removing all OSM layers automatically after disabling OSM plugin in plugin manager
- some dialogs has been renamed
- other small bugfixes ...

Thanks.



git-svn-id: http://svn.osgeo.org/qgis/trunk@11603 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Sep 9, 2009
1 parent 89f56ae commit 29fdde2
Show file tree
Hide file tree
Showing 28 changed files with 710 additions and 285 deletions.
16 changes: 13 additions & 3 deletions python/plugins/osm/OsmAddRelationDlg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from qgis.gui import *

import sqlite3
import unicodedata



Expand Down Expand Up @@ -47,9 +48,9 @@ def __init__(self, plugin, newRelationFirstMember=None, relationToEdit=None):

# set icons for tool buttons (identify,move,createPoint,createLine,createPolygon)
self.chooseMemberButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_identify.png"))
self.removeMemberButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_remove.png"))
self.removeTagButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_remove.png"))
self.loadStandardTagsButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_star.png"))
self.removeMemberButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeMember.png"))
self.removeTagButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeTag.png"))
self.loadStandardTagsButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_generateTags.png"))
self.typeInfoButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_questionMark.png"))

self.info = dict()
Expand Down Expand Up @@ -101,6 +102,7 @@ def __init__(self, plugin, newRelationFirstMember=None, relationToEdit=None):
self.relTagsTable.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.relMembersTable.setSelectionBehavior(QAbstractItemView.SelectRows)
self.relTagsTable.setSelectionBehavior(QAbstractItemView.SelectRows)
self.typeInfoButton.setEnabled(False)


def connectDlgSignals(self):
Expand Down Expand Up @@ -283,6 +285,11 @@ def __onTagsCellChanged(self,row,column):

# adding new tag to table
if column==0:

# only ascii keys are allowed
nkfd_form=unicodedata.normalize('NFKD', unicode(key.toUtf8(),'utf-8'))
key_only_ascii=nkfd_form.encode('ASCII', 'ignore')

newLastRow = row+1
self.relTagsTable.setRowCount(row+2)
self.relTagsTable.setItem(newLastRow,0,QTableWidgetItem(self.newTagLabel))
Expand All @@ -292,6 +299,7 @@ def __onTagsCellChanged(self,row,column):

self.relTagsTable.item(row,0).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.relTagsTable.item(row,1).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable)
self.relTagsTable.item(row,0).setText(QString(key_only_ascii))


def __onMembersCellChanged(self,row,column):
Expand Down Expand Up @@ -426,8 +434,10 @@ def __onTypeSelectionChanged(self,typeName):
# if non-standard typename was set up, loadStandardTagsButton is useless
if typeName.toAscii().data() in self.relationTypes:
self.loadStandardTagsButton.setEnabled(True)
self.typeInfoButton.setEnabled(True)
else:
self.loadStandardTagsButton.setEnabled(False)
self.typeInfoButton.setEnabled(False)


def determineSuitableMemberRoles(self,relType):
Expand Down
99 changes: 73 additions & 26 deletions python/plugins/osm/OsmDatabaseManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def __init__(self,plugin):
Initializes inner structures of OsmDatabaseManager and connect signals to appropriate slots.
"""

self.plugin=plugin
self.canvas=plugin.canvas

Expand All @@ -50,6 +49,7 @@ def __init__(self,plugin):
self.polygonLayers={}
self.currentKey=None
self.removing=False
self.mapReg=QgsMapLayerRegistry.instance()

QObject.connect(self.plugin.iface,SIGNAL("currentLayerChanged(QgsMapLayer*)"),self.currLayerChanged)
QObject.connect(QgsMapLayerRegistry.instance(),SIGNAL("layerWillBeRemoved(QString)"),self.layerRemoved)
Expand All @@ -72,14 +72,18 @@ def currLayerChanged(self,layer):
# set new currentKey and tell all other plugin components
if not layer:
self.currentKey=None
self.plugin.undoredo.databaseChanged(None)
self.plugin.dockWidget.databaseChanged(None)
if self.plugin.undoredo<>None:
self.plugin.undoredo.databaseChanged(None)
if self.plugin.dockWidget<>None:
self.plugin.dockWidget.databaseChanged(None)
return

if layer.type() != QgsMapLayer.VectorLayer or layer.dataProvider().name()<>"osm":
self.currentKey=None
self.plugin.undoredo.databaseChanged(None)
self.plugin.dockWidget.databaseChanged(None)
if self.plugin.undoredo<>None:
self.plugin.undoredo.databaseChanged(None)
if self.plugin.dockWidget<>None:
self.plugin.dockWidget.databaseChanged(None)
return

# find out filename of new current database
Expand All @@ -89,12 +93,18 @@ def currLayerChanged(self,layer):

if dbFileName not in self.dbConns.keys():
self.currentKey=None
if self.plugin.undoredo<>None:
self.plugin.undoredo.databaseChanged(None)
if self.plugin.dockWidget<>None:
self.plugin.dockWidget.databaseChanged(None)
return

if dbFileName.toLatin1().data()<>self.currentKey:
self.currentKey=dbFileName.toLatin1().data()
self.plugin.undoredo.databaseChanged(self.currentKey)
self.plugin.dockWidget.databaseChanged(self.currentKey)
if self.plugin.undoredo<>None:
self.plugin.undoredo.databaseChanged(self.currentKey)
if self.plugin.dockWidget<>None:
self.plugin.dockWidget.databaseChanged(self.currentKey)


def layerRemoved(self,layerID):
Expand All @@ -112,7 +122,7 @@ def layerRemoved(self,layerID):
return

# get appropriate qgsvectorlayer object
layer=QgsMapLayerRegistry.instance().mapLayer(layerID)
layer=self.mapReg.mapLayer(layerID)

if not layer:
return # strange situation
Expand All @@ -126,21 +136,49 @@ def layerRemoved(self,layerID):
dbFileName=layerSource.left(pos)+".db"
key=dbFileName.toLatin1().data()

# remove all map layers that belong to dbFileName database
# remove map layers that belong to dbFileName database
if key in self.lineLayers.keys() and layer.getLayerID()==self.lineLayers[key].getLayerID():
del self.lineLayers[key]

elif key in self.pointLayers.keys() and layer.getLayerID()==self.pointLayers[key].getLayerID():
del self.pointLayers[key]
if key in self.lineLayers.keys():
if self.lineLayers[key]:
lineLayID=self.lineLayers[key].getLayerID()
self.mapReg.removeMapLayer(lineLayID,True)

elif key in self.polygonLayers.keys() and layer.getLayerID()==self.polygonLayers[key].getLayerID():
del self.polygonLayers[key]
if key in self.pointLayers.keys():
if self.pointLayers[key]:
pointLayID=self.pointLayers[key].getLayerID()
self.mapReg.removeMapLayer(pointLayID,True)

if key in self.dbConns.keys():
del self.dbConns[key]


def removeAllOsmLayers(self):

# remove all map layers with osm provider set
self.removing=True
if layer.getLayerID()<>self.pointLayers[key].getLayerID():
QgsMapLayerRegistry.instance().removeMapLayer(self.pointLayers[key].getLayerID(),True)
if layer.getLayerID()<>self.lineLayers[key].getLayerID():
QgsMapLayerRegistry.instance().removeMapLayer(self.lineLayers[key].getLayerID(),True)
if layer.getLayerID()<>self.polygonLayers[key].getLayerID():
QgsMapLayerRegistry.instance().removeMapLayer(self.polygonLayers[key].getLayerID(),True)
self.removing=False
allLayers=QgsMapLayerRegistry.instance().mapLayers()

# removed map items of key <dbFileName>
del self.dbConns[key]
del self.pointLayers[key]
del self.lineLayers[key]
del self.polygonLayers[key]
for ix in allLayers.keys():

layer=allLayers[ix] # layer object
if not layer:
continue

if layer.type()==QgsMapLayer.VectorLayer and layer.dataProvider().name()=="osm":
QgsMapLayerRegistry.instance().removeMapLayer(layer.getLayerID(),True)

self.dbConns={} # map dbFileName->sqlite3ConnectionObject
self.pointLayers={}
self.lineLayers={}
self.polygonLayers={}
self.currentKey=None
self.removing=False


def addDatabase(self,dbFileName,pointLayer,lineLayer,polygonLayer):
Expand Down Expand Up @@ -1661,7 +1699,7 @@ def getFeatureTags(self,featId,featType):
,{"objId":str(featId),"objType":str(osmType)})

for tagRec in c:
tags.append((tagRec[0],tagRec[1]))
tags.append((tagRec[0],tagRec[1].encode('utf-8')))

c.close()
return tags
Expand Down Expand Up @@ -1709,7 +1747,7 @@ def setTagValue(self,featId,featType,tagKey,tagValue):
c=self.getConnection().cursor()

c.execute("update tag set val=:tagVal where object_id=:objId and object_type=:objType and key=:tagKey and u=1"
,{"tagVal":tagValue,"objId":str(featId),"objType":osmType,"tagKey":tagKey})
,{"tagVal":unicode(tagValue,'utf-8'),"objId":str(featId),"objType":osmType,"tagKey":tagKey})

for rec in c:
val=rec[0]
Expand Down Expand Up @@ -1889,7 +1927,7 @@ def changeTagValue(self,featId,featType,key,value):
osmType=self.convertToOsmType(featType)
c=self.getConnection().cursor()
c.execute("update tag set val=:val where object_id=:objId and object_type=:objType and key=:key and u=1"
,{"val":value,"objId":str(featId),"objType":osmType,"key":key})
,{"val":unicode(value,'utf-8'),"objId":str(featId),"objType":osmType,"key":str(key)})
c.close()
self.commit()

Expand All @@ -1902,6 +1940,11 @@ def isTagDefined(self,featId,featType,key):
@return True if given tag already exists for given feature; False otherwise
"""

if key==None:
return False
if len(key)==0:
return False

tagEx=False
osmType=self.convertToOsmType(featType)
c=self.getConnection().cursor()
Expand Down Expand Up @@ -1990,10 +2033,14 @@ def insertTag(self,featId,featType,key,value, doCommit=True):
@param doCommit if True then commit is performed after tag insertion
"""

val=''
if value<>None:
val=unicode(value,'utf-8')

osmType=self.convertToOsmType(featType)
c=self.getConnection().cursor()
c.execute("insert into tag (object_id, object_type, key, val) values (:objId, :objType, :key, :val)"
,{"objId":str(featId),"objType":osmType,"key":key,"val":value})
,{"objId":str(featId),"objType":osmType,"key":str(key),"val":val})
c.close()
if doCommit:
self.commit()
Expand Down Expand Up @@ -2029,7 +2076,7 @@ def removeTag(self,featId,featType,key):
osmType=self.convertToOsmType(featType)
c=self.getConnection().cursor()
c.execute("delete from tag where object_id=:objId and object_type=:objType and key=:key and u=1"
,{"objId":str(featId),"objType":osmType,"key":key})
,{"objId":str(featId),"objType":osmType,"key":str(key)})
c.close()
self.commit()

Expand Down
28 changes: 18 additions & 10 deletions python/plugins/osm/OsmFeatureDW.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-99
"""@package OsmFeatureDW
This module is descendant of "OSM Feature" dockable widget and makes user able
to view and edit information on selected OSM feature.
Expand All @@ -24,6 +25,7 @@
from map_tools.OsmCreatePolygonMT import OsmCreatePolygonMT
from map_tools.OsmMoveMT import OsmMoveMT
from map_tools.OsmIdentifyMT import OsmIdentifyMT
import unicodedata


class OsmFeatureDW(QDockWidget, Ui_OsmFeatureDW, object):
Expand Down Expand Up @@ -54,8 +56,8 @@ def __init__(self, plugin):
self.createLineButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createLine.png"))
self.createPolygonButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createPolygon.png"))
self.createRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createRelation.png"))
self.removeButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_remove.png"))
self.deleteTagsButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_remove.png"))
self.removeButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeFeat.png"))
self.deleteTagsButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeTag.png"))
self.undoButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_undo.png"))
self.redoButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_redo.png"))
self.addRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_addRelation.png"))
Expand Down Expand Up @@ -432,7 +434,7 @@ def __onTagsCellChanged(self,row,column):

# store tag's change into database
self.plugin.undoredo.startAction("Change tag value.")
self.plugin.dbm.changeTagValue(self.feature.id(),self.featureType,key.toUtf8().data(),value.toUtf8().data())
self.plugin.dbm.changeTagValue(self.feature.id(),self.featureType,key.toAscii().data(),value.toUtf8())
else:
key = self.tagTable.item(row,0).text()
if key=="" or key==self.newTagLabel:
Expand All @@ -441,23 +443,28 @@ def __onTagsCellChanged(self,row,column):
# adding new tag and setting its key
if column==0:

# only ascii keys are allowed
nkfd_form=unicodedata.normalize('NFKD', unicode(key.toUtf8(),'utf-8'))
key_only_ascii=nkfd_form.encode('ASCII', 'ignore')

# store it into database
isAlreadyDef=self.plugin.dbm.isTagDefined(self.feature.id(),self.featureType,key.toUtf8().data())
isAlreadyDef=self.plugin.dbm.isTagDefined(self.feature.id(),self.featureType,key_only_ascii)
if isAlreadyDef:
# such a key already exists for this relation
self.tagTable.setItem(row,0,QTableWidgetItem(self.newTagLabel))
QMessageBox.information(self, self.tr("OSM Feature Dock Widget")
,self.tr("Property '%1' cannot be added twice.").arg(key.toUtf8().data()))
,self.tr(QString("Property '").append(key_only_ascii).append("' cannot be added twice.")))
return

# well, insert new tag into database
self.plugin.undoredo.startAction("Insert new tag.")
self.plugin.dbm.insertTag(self.feature.id(),self.featureType,key.toUtf8().data(),'')
self.plugin.dbm.insertTag(self.feature.id(),self.featureType,key_only_ascii,None)

self.__tagsLoaded=False

self.tagTable.item(row,0).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.tagTable.item(row,1).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable)
self.tagTable.item(row,0).setText(QString(key_only_ascii))

newLastRow = row+1
self.tagTable.setRowCount(row+2)
Expand Down Expand Up @@ -488,7 +495,8 @@ def __onTagsCellChanged(self,row,column):
self.plugin.dbm.recacheAffectedNow(affected)

# refresh map canvas so that changes take effect
self.plugin.canvas.refresh()
if column==1:
self.plugin.canvas.refresh()


def __onTagsItemDoubleClicked(self,item):
Expand Down Expand Up @@ -1236,7 +1244,7 @@ def removeSelectedTags(self):
self.plugin.dbm.changeRelationStatus(self.feature.id(),'N','U')

# perform tag removing
self.plugin.dbm.removeTag(self.feature.id(),self.featureType,key.toUtf8().data())
self.plugin.dbm.removeTag(self.feature.id(),self.featureType,key.toAscii().data())

self.tagTable.removeRow(ix)

Expand Down Expand Up @@ -1425,8 +1433,8 @@ def __loadFeatureTags(self,featId,featType):
self.tagTable.setHorizontalHeaderItem(1,QTableWidgetItem("Value"))

for i in range(0,rowCount):
self.tagTable.setItem(i,0,QTableWidgetItem(tableData[i][0]))
self.tagTable.setItem(i,1,QTableWidgetItem(tableData[i][1]))
self.tagTable.setItem(i,0,QTableWidgetItem(QString.fromUtf8(tableData[i][0])))
self.tagTable.setItem(i,1,QTableWidgetItem(QString.fromUtf8(tableData[i][1])))
self.tagTable.item(i,0).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.tagTable.item(i,1).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable)

Expand Down
7 changes: 3 additions & 4 deletions python/plugins/osm/OsmLoadDlg.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,9 @@ def onOK(self):

st=self.styles[self.styleCombo.currentIndex()]
if st=="Small scale":
rect=QgsRectangle(midX-rX/6,midY-rY/6,midX+rX/6,midY+rY/6)
rect=QgsRectangle(midX-rX/5,midY-rY/5,midX+rX/5,midY+rY/5)
elif st=="Medium scale":
rect=QgsRectangle(midX-rX/3,midY-rY/3,midX+rX/3,midY+rY/3)
else:
rect=QgsRectangle(midX-rX/1.2,midY-rY/1.2,midX+rX/1.2,midY+rY/1.2)
rect=QgsRectangle(midX-rX/2,midY-rY/2,midX+rX/2,midY+rY/2)

self.canvas.setExtent(rect)
self.canvas.refresh()
Expand Down Expand Up @@ -310,6 +308,7 @@ def event(self, e):
self.progress = None
QMessageBox.information(self,"Error",QString("Failed to load layers: %1")
.arg(self.property("osm_failure").toString()))
self.buttonBox.setEnabled(True)

qApp.processEvents()
return QDialog.event(self,e)
Expand Down
Loading

0 comments on commit 29fdde2

Please sign in to comment.