Skip to content
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

Closed
qgib opened this issue Apr 20, 2018 · 7 comments
Closed

QgsProject.instance().geometry().asPolyline() not working on QGIS 3 #26668

qgib opened this issue Apr 20, 2018 · 7 comments
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Expressions Related to the QGIS expression engine or specific expression functions

Comments

@qgib
Copy link
Contributor

qgib commented Apr 20, 2018

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

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)


@qgib
Copy link
Contributor Author

qgib commented Apr 20, 2018

Author Name: Nyall Dawson (@nyalldawson)


That's 2.x API - you need to change your code to use the 3 API


  • status_id was changed from Open to Closed
  • resolution was changed from to invalid

@qgib
Copy link
Contributor Author

qgib commented Apr 23, 2018

Author Name: Roger Gonzalez (Roger Gonzalez)


Nyall Dawson wrote:

That's 2.x API - you need to change your code to use the 3 API

That code uses 3 API...

@qgib
Copy link
Contributor Author

qgib commented Apr 23, 2018

Author Name: Jürgen Fischer (@jef-n)


  • description was changed from 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

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) to 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

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)

@qgib
Copy link
Contributor Author

qgib commented Apr 24, 2018

Author Name: Roger Gonzalez (Roger Gonzalez)


  • status_id was changed from Closed to Reopened

@qgib
Copy link
Contributor Author

qgib commented May 1, 2018

Author Name: Nyall Dawson (@nyalldawson)


Can you share your data?


  • status_id was changed from Reopened to Feedback

@qgib
Copy link
Contributor Author

qgib commented May 2, 2018

Author Name: Roger Gonzalez (Roger Gonzalez)


Nyall Dawson wrote:

Can you share your data?

Sure, here is it

And this is the necessary code

layer_name= 'Test'
#layerMT1=QgsMapLayerRegistry.instance().mapLayersByName(layer_name)[0] ## for QGIS 2
layerMT1=QgsProject.instance().mapLayersByName(layer_name)[0] ## for QGIS 3

lineasMT = layerMT1.getFeatures()
for lineaMT in lineasMT:
    line = lineaMT.geometry().asPolyline()
    # Read line geometry
    print(line)


  • 12662 was configured as Test.zip
  • 12663 was configured as Error.png

  • Test.zip (Roger Gonzalez) - Shape Test
  • Error.png (Roger Gonzalez) - Error Screenshot

@qgib
Copy link
Contributor Author

qgib commented May 3, 2018

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:
line = lineaMT.geometry().asMultiPolyline()[0]
# Read line geometry
print(line)

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.


  • status_id was changed from Feedback to Closed

@qgib qgib closed this as completed May 3, 2018
@qgib qgib added Bug Either a bug report, or a bug fix. Let's hope for the latter! Expressions Related to the QGIS expression engine or specific expression functions labels May 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Expressions Related to the QGIS expression engine or specific expression functions
Projects
None yet
Development

No branches or pull requests

1 participant