Geographic Information Systems Asked by openSourcerer on May 19, 2021
ArcGIS user here. Since we’re all working from home now, ArcMap only works 2 VPN’s deep and is basically garbage to us now at our office, so I’m going all in with QGIS.
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) and is there any good resource to translate to QGIS and it’s Python dialect for ArcMap/ArcPy users?
QGIS version 3.12, Python version 3.7.6
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())
Answered by openSourcerer on May 19, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP