-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QgsProject.instance().geometry().asPolyline() not working on QGIS 3 #26668
Comments
Author Name: Nyall Dawson (@nyalldawson) That's 2.x API - you need to change your code to use the 3 API
|
Author Name: Roger Gonzalez (Roger Gonzalez) Nyall Dawson wrote:
That code uses 3 API... |
Author Name: Jürgen Fischer (@jef-n)
def getAttributeIndex(aLayer, attrName): layer_name= 'L_MT_IND' lineasMT = layerMT1.getFeatures() def getAttributeIndex(aLayer, attrName): if len(attrName) > 10 and aLayer.storageType() == 'ESRI Shapefile': iface.messageBar().pushMessage('Error', 'For ESRI Shapefiles, the maximum length of any attribute name is 10. Please choose a shorter attribute name.', level=QgsMessageBar.CRITICAL) return -3 AttrIdx = aLayer.dataProvider().fieldNameIndex(attrName) if AttrIdx == -1: caps = aLayer.dataProvider().capabilities() if caps & QgsVectorDataProvider.AddAttributes: res = aLayer.dataProvider().addAttributes([QgsField(attrName, QVariant.String)]) AttrIdx = aLayer.dataProvider().fieldNameIndex(attrName) aLayer.updateFields() if AttrIdx == -1: iface.messageBar().pushMessage('Error', 'Failed to create attribute!', level=QgsMessageBar.CRITICAL) return -1 else: iface.messageBar().pushMessage('Error', 'Failed to add attribute!', level=QgsMessageBar.CRITICAL) return -1 return AttrIdx layer_name= 'L_MT_IND' #layerMT1=QgsMapLayerRegistry.instance().mapLayersByName(layer_name)[0] ## for QGIS 2 layerMT1=QgsProject.instance().mapLayersByName(layer_name)[0] ## for QGIS 3 idx = getAttributeIndex(layerMT1, "DSSName") lineasMT = layerMT1.getFeatures() for lineaMT in lineasMT: line = lineaMT.geometry().asPolyline() # Read line geometry print(line) |
Author Name: Roger Gonzalez (Roger Gonzalez)
|
Author Name: Nyall Dawson (@nyalldawson) Can you share your data?
|
Author Name: Roger Gonzalez (Roger Gonzalez) Nyall Dawson wrote:
Sure, here is it And this is the necessary code
|
Author Name: Nyall Dawson (@nyalldawson) Ah - these are multipolyline geometries in 3.0, not polylines. So your code needs to be: for lineaMT in lineasMT: QGIS 3.0 always pulls in shapefiles using multi-geometry types for line or polygon layers. This change was made due to shapefiles not having strict constraints on the single/multi type feature geometries for lines and polygons -- so a layer reporting that it contained LineString geometries may have MultiLineString geometries mixed in there too. This breaks many assumptions in QGIS code, so for consistency it was decided to always return multi-geometries for lines/polygons for shapefiles.
|
Author Name: Roger Gonzalez (Roger Gonzalez)
Original Redmine Issue: 18781
Affected QGIS version: 3.0.1
Redmine category:geometry
The next code only works on 2.xx versions of QGIS, returning empty list when it should export Polylines coordinates like in QGIS 2.xx
The text was updated successfully, but these errors were encountered: