Skip to content

Commit

Permalink
Layer order
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Bach committed Nov 13, 2015
1 parent 1426a09 commit df63bb6
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 40 deletions.
128 changes: 128 additions & 0 deletions assets/LayerTree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"Name": "Root",
"IsGroup": true,
"TableName": "",
"Nodes": [
{
"Name": "PAP_APPROUVE",
"IsGroup": false,
"TableName": "PAG.PAP_APPROUVE",
"Nodes": []
},
{
"Name": "ZONES_QE",
"IsGroup": false,
"TableName": "PAG.ZONES_QE",
"Nodes": []
},
{
"Name": "Group1",
"IsGroup": true,
"TableName": "",
"Nodes": [
{
"Name": "GABARIT_A_SAUV_POINT",
"IsGroup": false,
"TableName": "PAG.GABARIT_A_SAUV_POINT",
"Nodes": []
},
{
"Name": "CONST_A_CONS_POINT",
"IsGroup": false,
"TableName": "PAG.CONST_A_CONS_POINT",
"Nodes": []
},
{
"Name": "BIOTOPE_POINT",
"IsGroup": false,
"TableName": "ARTIKEL17.BIOTOPE_POINT",
"Nodes": []
},
{
"Name": "ALIGN_A_RESP",
"IsGroup": false,
"TableName": "PAG.ALIGN_A_RESP",
"Nodes": []
},
{
"Name": "BIOTOPE_LIGNE",
"IsGroup": false,
"TableName": "ARTIKEL17.BIOTOPE_LIGNE",
"Nodes": []
},
{
"Name": "BIOTOPE_POLY",
"IsGroup": false,
"TableName": "ARTIKEL17.BIOTOPE_POLY",
"Nodes": []
},
{
"Name": "ZONE_SERV_URB",
"IsGroup": false,
"TableName": "PAG.ZONE_SERV_URB",
"Nodes": []
},
{
"Name": "COULOIRS_ET_ESP_RES",
"IsGroup": false,
"TableName": "PAG.COULOIRS_ET_ESP_RES",
"Nodes": []
},
{
"Name": "ZAD",
"IsGroup": false,
"TableName": "PAG.ZAD",
"Nodes": []
},
{
"Name": "NQ_PAP",
"IsGroup": false,
"TableName": "PAG.NQ_PAP",
"Nodes": []
},
{
"Name": "ZONES_SUPERPOSEES",
"IsGroup": false,
"TableName": "PAG.ZONES_SUPERPOSEES",
"Nodes": []
},
{
"Name": "CONST_A_CONS_POLY",
"IsGroup": false,
"TableName": "PAG.CONST_A_CONS_POLY",
"Nodes": []
},
{
"Name": "GABARIT_A_SAUV_POLY",
"IsGroup": false,
"TableName": "PAG.GABARIT_A_SAUV_POLY",
"Nodes": []
},
{
"Name": "EMPLAC_STAT",
"IsGroup": false,
"TableName": "PAG.EMPLAC_STAT",
"Nodes": []
},
{
"Name": "ZONAGE",
"IsGroup": false,
"TableName": "PAG.ZONAGE",
"Nodes": []
},
{
"Name": "PERIMETRE",
"IsGroup": false,
"TableName": "PAG.PERIMETRE",
"Nodes": []
}
]
},
{
"Name": "PCN",
"IsGroup": false,
"TableName": "PAG.PCN",
"Nodes": []
}
]
}
95 changes: 55 additions & 40 deletions project.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,38 +410,25 @@ def _updateMapLayers(self):
Update layers attributes editors and add missing layers to the TOC
'''

# Map layers in the TOC
maplayers = QgsMapLayerRegistry.instance().mapLayers()
# Get rules config
config_path = os.path.join(PagLuxembourg.main.plugin_dir,
'assets',
'LayerTree.json')

stylize = StylizeProject()
f = open(config_path, 'r')
config_file = f.read()
config = json.loads(config_file)
f.close()

# Iterates through XSD types
for type in main.xsd_schema.types:
uri = self.getTypeUri(type)
found = False

# Check whether a layer with type data source exists in the map
for k,v in maplayers.iteritems():
if self.compareURIs(v.source(), uri):
found = True
layer = v
break

# If not found, add to the map
if not found:
layer = QgsVectorLayer(uri, type.friendlyName(), 'spatialite')
self._addMapLayer(layer, type)

# Updates layers style
stylize.stylizeLayer(layer,type)

# Update attributes editors
self._updateLayerEditors(layer, type)
main.qgis_interface.messageBar().clearWidgets()

# Process root node tree
self._updateLayerTreeNode(config, config)

# Add WMS basemap layer
ortho_url = 'url=http://wsinspire.geoportail.lu/oi&SLegend=0&crs=EPSG:2169&dpiMode=7&featureCount=10&format=image/jpeg&layers=1&styles='
ortho_found = False
for k,v in maplayers.iteritems():
for k,v in QgsMapLayerRegistry.instance().mapLayers().iteritems():
if v.source() == ortho_url:
ortho_found = True
break
Expand All @@ -455,28 +442,55 @@ def _updateMapLayers(self):
# Add topology rules
TopologyChecker(None).updateProjectRules()

def _addMapLayer(self, layer, type):
def _updateLayerTreeNode(self, node, parentnode):
'''
Adds a layer to the map
Update a layer tree node, a lyer group
:param layer: The layer to update
:type layer: QgsVectorLayer
:param node: The current node to update
:type node: dict
:param type: XSD schema type
:type type: PAGType
:param node: The parent node to update
:type node: dict
'''

# Add to map
QgsMapLayerRegistry.instance().addMapLayer(layer)
parent = QgsProject.instance().layerTreeRoot()

# Add to the correct topic group
legend = main.qgis_interface.legendInterface()
if parentnode['Name'] != 'Root':
parent = parent.findGroup(parentnode['Name'])

if type.topic() not in legend.groups():
legend.addGroup(type.topic())
treenode = parent.findGroup(node['Name']) if node['Name'] != 'Root' else QgsProject.instance().layerTreeRoot()

if treenode is None:
treenode = parent.addGroup(node['Name'])

group_index = legend.groups().index(type.topic())
legend.moveLayer(layer,group_index)
stylize = StylizeProject()

for child in node['Nodes']:
if child['IsGroup']:
self._updateLayerTreeNode(child, node)
else:
xsd_type = main.xsd_schema.getTypeFromTableName(child['TableName'])

# Type not found in XSD
if xsd_type is None:
main.qgis_interface.messageBar().pushSuccess(QCoreApplication.translate('Project','Error'),
QCoreApplication.translate('Project','Type not found in XSD : {}').format(child['TableName']))
continue

layer = self.getLayer(xsd_type)

# Layer is in the TOC
if layer is None:
uri = self.getTypeUri(xsd_type)
layer = QgsVectorLayer(uri, child['Name'], 'spatialite')
QgsMapLayerRegistry.instance().addMapLayer(layer, False)
treenode.addLayer(layer)

# Updates layers style
stylize.stylizeLayer(layer, xsd_type)

# Update attributes editors
self._updateLayerEditors(layer, xsd_type)

def getUriInfos(self, uri):
'''
Expand All @@ -488,6 +502,7 @@ def getUriInfos(self, uri):
:returns: Database and table name
:rtype: tuple(QString, QString)
'''

db=''
table=''
split = uri.split(' ')
Expand Down
14 changes: 14 additions & 0 deletions schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ def getType(self, typename):

return None

def getTypeFromTableName(self, tablename):
'''
Get a type from the table name
:param tablename: The table name (ex : ARTIKEL17.BIOTOPE_LIGNE)
:type tablename: str, QString
'''

for type in self.types:
if type.name == tablename:
return type

return None

def _getTopicMembers(self, typename, xml_root, ns):
'''
Parse XML node
Expand Down
1 change: 1 addition & 0 deletions widgets/topology/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def updateProjectRules(self):
f = open(config_path, 'r')
config_file = f.read()
config = json.loads(config_file)
f.close()

# Get project rules
project_rules_count,topol_section_exists = QgsProject.instance().readNumEntry(TOPOL_SECTION, "/testCount" );
Expand Down

0 comments on commit df63bb6

Please sign in to comment.