2

I would like to know, what is the equivalent of ArcPy's FeatureClassToNumPyArray which does what the name suggests (grabs attribute table fields, length, area, x , y, etc and loads it to a structured numpy array)?

QGIS version 3.12, Python version 3.7.6

0

2 Answers 2

2

You will want to convert your featureclass to a Geopandas geodataframe. Geopandas grabs all the attributes and assigns a geometry field in one line using geopandas.read_file(). Once your featureclass has been converted to a geodataframe you can do a variety of spatial operations using Geopandas and countless attribute manipulations using standard Pandas

For example:

import geopandas as gpd

gdf = gpd.read_file('/path/to/your/geodatabase.gdb', driver='FileGDB', layer='featureclass')
print(gdf)

     Id  ...                                           geometry
0     0  ...  MULTIPOLYGON (((574822.862 463991.125, 575346....
1     0  ...  MULTIPOLYGON (((-315513.947 427995.640, -31580...
2     0  ...  MULTIPOLYGON (((-130317.033 1186430.700, -1304...
3     0  ...  MULTIPOLYGON (((-271811.830 1121101.707, -2720...
4     0  ...  MULTIPOLYGON (((-290662.084 1149661.257, -2909...
..   ..  ...                                                ...
107   0  ...  MULTIPOLYGON (((265211.850 1218091.959, 260277...
108   0  ...  MULTIPOLYGON (((191176.499 1215622.191, 191370...
109   0  ...  MULTIPOLYGON (((48520.918 1136776.587, 48569.5...
110   0  ...  MULTIPOLYGON (((28547.064 1136665.373, 33540.5...
111   0  ...  MULTIPOLYGON (((-70893.287 1178620.142, -70964...
0

So copying and pasting from the PyQGIS cookbook I have a function that gets the attribute table as a pandas dataframe, the geometry bit could easily be uncommented and loaded into the DF as well.

import pandas as pd
def getAttem(layer= iface.activeLayer()):
    """retrieve every feature with its attributes"""
    fldz = [field.name() for field in layer.fields()]
    attrz = []
    features = layer.getFeatures()
    for feature in features:
        # print("Feature ID: ", feature.id())
        # # fetch geometry
        # # show some information about the feature geometry
        # geom = feature.geometry()
        # geomSingleType = QgsWkbTypes.isSingleType(geom.wkbType())
        # if geom.type() == QgsWkbTypes.PointGeometry:
        #     # the geometry type can be of single or multi type
        #     if geomSingleType:
        #         x = geom.asPoint()
        #         print("Point: ", x)
        #     else:
        #         x = geom.asMultiPoint()
        #         print("MultiPoint: ", x)
        # elif geom.type() == QgsWkbTypes.LineGeometry:
        #     if geomSingleType:
        #         x = geom.asPolyline()
        #         print("Line: ", x, "length: ", geom.length())
        #     else:
        #         x = geom.asMultiPolyline()
        #         print("MultiLine: ", x, "length: ", geom.length())
        # elif geom.type() == QgsWkbTypes.PolygonGeometry:
        #     if geomSingleType:
        #         x = geom.asPolygon()
        #         print("Polygon: ", x, "Area: ", geom.area())
        #     else:
        #         x = geom.asMultiPolygon()
        #         print("MultiPolygon: ", x, "Area: ", geom.area())
        # else:
        #     print("Unknown or invalid geometry")
        # fetch attributes
        attrz += [feature.attributes()]
        # attrs is a list. It contains all the attribute values of this feature
        #print(attrs)
    attr = pd.DataFrame(attrz,columns = fldz)
    return attr
print(getAttem())
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.