Created
September 1, 2014 07:23
-
-
Save jtornero/fbc9928720da2de364f1 to your computer and use it in GitHub Desktop.
Check for layer compatibility - QGIS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def checkForTransectizerLayer(self, selectedByCombo = True): | |
""" | |
This slto checks if a layer is appropiate for woriking with | |
Transectizer. A such layer should have the following | |
Attributes: | |
=========== | |
survey(string, 20): A descripive name of survey/transect. | |
station:(string, 10): A short descriptive prefix/name for the stations. | |
stnnum(int): Sequential/arbitrary number for the station. | |
stnlat(double): Latitude, , in decimal degrees, for the station. | |
stnlon(double): Longitude, in decimal degrees, for the station. | |
stnobs(string, 254): Observations/remarks for the station. | |
Checking a layer is done field by field, against a field list | |
""" | |
# Creating the list of fields to be compared | |
surveyField = QgsField(name = 'survey', type = 10,\ | |
typeName = 'string', len = 20) | |
stationField = QgsField(name = 'station', type = 10,\ | |
typeName = 'string', len = 10) | |
stnnumField = QgsField(name = 'stnnum', type =2,\ | |
typeName = 'integer', len = 10, prec = 0) | |
stnlatField = QgsField(name = 'stnlat', type = 6,\ | |
typeName = 'double', len = 20, prec = 5) | |
stnlonField = QgsField(name = 'stnlon', type = 6,\ | |
typeName = 'double', len = 20, prec = 5) | |
stnobsField = QgsField(name = 'stnobs', type = 10,\ | |
typeName = 'string', len = 254) | |
fieldList = (surveyField, stationField, stnnumField,\ | |
stnlatField, stnlonField, stnobsField) | |
# Now we get the fields from the layer to be checked | |
layerName = self.dlg.ui.layersCombo.currentText() | |
layer = QgsMapLayerRegistry.instance().mapLayersByName(layerName)[0] | |
provider = layer.dataProvider() | |
fieldsToCheck = provider.fields() | |
# A valid layer for the transectizer will have ALL of the defined | |
# fields. Direct comparation on fields is possible. | |
layerOK = all(field in fieldsToCheck for field in fieldList) | |
# Time to manage the results of the field tests | |
msg = QMessageBox() | |
# If our layer is compatible with transectizer (it has all the | |
# required fields), we just return it | |
if layerOK: | |
if selectedByCombo: | |
msg.setText(self.tr("<center>%s is a valid layer<br>for Transectizer to work<center>" %layerName)) | |
msg.exec_() | |
return layer | |
# If not, we offer the user the chance | |
# of creating them in the chosen layer | |
else: | |
msg.setText(self.tr("""<center>It looks like the selected layer<br> | |
has not the fields required for Transectizer to work.<br> | |
Do you want them to be added to your layer?<center>""")) | |
btn1 = msg.addButton(self.tr("Add and continue"), QMessageBox.YesRole) | |
btn2 = msg.addButton(self.tr("Cancel"), QMessageBox.NoRole) | |
msg.exec_() | |
# Creation of the new fields in the layer | |
if msg.clickedButton() == btn1: | |
layer.startEditing() | |
layer.dataProvider().addAttributes(fieldList) | |
layer.commitChanges() | |
layer.reload() | |
return layer | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment