Skip to main content
16 votes
Accepted

Getting dictionary for each feature with their attribute values using PyQGIS

The QgsJsonUtils class has exportAttributes method for getting attributes as a dict. Solution 1: Making a dictionary including feature "id" as key, attributes map as value: {feature1.id: {...
Kadir Şahbaz's user avatar
11 votes
Accepted

Converting full road names to abbreviation using ArcGIS field calculator?

Use a dictionary and if/else with list comprehension to replace the words: Pre-logic: def replacename( names, new_names): d = {'Lane':'Ln', 'Road':'Rd', 'Street':'St'} return ' '.join([d[...
Bera's user avatar
  • 77.8k
11 votes

Getting dictionary for each feature with their attribute values using PyQGIS

The snippet below should help you. It just prints a dictionary for each feature to the console, but you could do something else with the feature attribute dictionaries if you wanted. from qgis.utils ...
Ben W's user avatar
  • 23.1k
8 votes
Accepted

Summing Values for Each Item in a Dictionary

Iterate over the dict items and construct a new dict from the key and sum of the values: >>> slackloopLength = {'abc': [1,2,4,7], 'def': [1,3,5,7,9]} >>> {k: sum(v) for k, v in ...
Spacedman's user avatar
  • 66.5k
7 votes

AttributeError when calculating area in hectares in PyQGIS

Since processing.runAndLoadResults gives you a dictionary, you get the error. You have to use processing.run to get a layer and add it to QGIS manually using QgsProject.instance().addMapLayer(clip). .....
menes's user avatar
  • 1,419
5 votes
Accepted

Parsing CSV data and updating a dictionary and creating a polyline

Don't use try:...except: when debugging. Don't use silent and overly broad exception handling ever. Some comments: You're not using the csv module. You import it, then never use it, instead you ...
user2856's user avatar
  • 69.6k
5 votes
Accepted

Using dictionary and cursors to calculate a field based on values from another table - KeyError [0]

search_feats_13_8 is a dict with OBJECTID as the key (part between []). upd_row is a tuple where the first element (referenced with [0]) is OBJECTID: What is being attempted here is to see if ...
Stephen Rauch's user avatar
5 votes

Getting dictionary for each feature with their attribute values using PyQGIS

# get the fields and features: fields = indiv.fields() features = indiv.getFeatures() # initialise an empty dict with each field name: attdict = {} for field in fields: attdict[field.name()] = [] ...
Spacedman's user avatar
  • 66.5k
5 votes

Getting dictionary for each feature with their attribute values using PyQGIS

The easiest way is using the underdog __geo_interface__, that was defined by Sean Gillies at https://gist.github.com/sgillies/2217756: layer = iface.activeLayer() for feature in layer.getFeatures(...
bugmenot123's user avatar
  • 11.3k
5 votes

Creating a dictionary using data types from user input

In arcpy.Describe(f) the variable f will evaluate to the filename only, without a directory. arcpy will search for that file in the 'current' directory, i.e. os.getcwd(). You need to either specify ...
Berend's user avatar
  • 4,777
5 votes

AttributeError when calculating area in hectares in PyQGIS

You get that error because layer has no method fieldNameIndex, but provider has. Use the following script: layer = iface.activeLayer() provider = layer.dataProvider() field = QgsField("area3&...
Kadir Şahbaz's user avatar
4 votes
Accepted

SearchCursor too many values to unpack

I am not sure which field you want your dictionary key to be, but for the answer I am providing I made it the joinldFld, you can easy change it by switching the index. valueDi = {x[0]: x[1] for x in ...
ziggy's user avatar
  • 4,576
4 votes
Accepted

Populating dictionary using ArcPy creates only 1 entry?

The problem is that each time around the loop you create a new dictionary called MK_dict over writing the previous one. MK_dict = {} for fc in fcs: COS = arcpy.Describe(fc).spatialReference.name ...
Ian Turton's user avatar
  • 83.2k
4 votes
Accepted

Create a line chart from a dictionary of values in google earth engine

I missed the bounty, but anyhow I got an answer. See if this is what you needed.. var createFC = function(element, list) { list = ee.List(list) // cast the 'accumulating' object element = ee....
Rodrigo E. Principe's user avatar
4 votes
Accepted

ArcPy UpdateCursor and Dictionary

You have some problems in the update for. 1) I don't understand why you want to iterate the dictionary, you already have the key in row[0]. 2) This comparison v == min(v) it is always false v is an ...
cabesuon's user avatar
  • 1,051
4 votes
Accepted

Computation with dictionary-values in GEE

The objects that you get on your computer (via the Earth Engine Code Editor or otherwise) are always represented as dictionaries (or JSON objects, if you like that term) but within Earth Engine they ...
Kevin Reid's user avatar
  • 10.5k
4 votes
Accepted

How to get dictionary value from reduceRegion in GEE Python

You call get() on the dictionary to get a value from the dictionary. If you know the type of the value, you can also call getArray(), getGeometry(), getNumber(), or getString(). That would return the ...
Daniel Wiell's user avatar
  • 14.8k
4 votes
Accepted

Converting CSV file with polygons in dictionary format into GeoDataFrame

I read the CSV file into a Pandas' DataFrame, extract the numbers from your boundary column, convert to shapely polygons, convert Pandas df to GeoPandas gdf: import pandas as pd import geopandas as ...
Bera's user avatar
  • 77.8k
4 votes
Accepted

Filter attribute information based on three different parameters and return ids using PyQGIS

I like to use collections defaultdict list for problems like this. (Your problem can be solved using a simple dictionary if you dont need to list the duplicates, just set one of them to 0.) from ...
Bera's user avatar
  • 77.8k
3 votes
Accepted

Using dictionary of file names and DD coordinates to create points in shapefile using shapely?

You don't need ArcPy here, simply use the geospatial pure Python modules as GeoPandas, Fiona, Shapely, pyshp (shapefile) or osgeo.ogr # the resulting dictionary dicto = {'IMG_2840.jpg': (39....
gene's user avatar
  • 55.4k
3 votes

Comparing Two Tables Using Arcpy

It is this line that is flawed: if row[0] == ipc[str(row[0])]: This is essentially asking if 10785823 = 24 which obviously it does not! I believe you are trying to ask if the dictionary ipc ...
Hornbydd's user avatar
  • 44.6k
3 votes
Accepted

Reformatting Dictionary Object to rows and columns using Google Earth Engine?

This may not be the only way to do it, but it works: /** YOUR CODE **/ // Call up WDPA data, filter var dataset = ee.FeatureCollection('WCMC/WDPA/current/polygons') .filter(ee.Filter....
Rodrigo E. Principe's user avatar
3 votes
Accepted

Populating a Dictionary with an Update Cursor "Typeerror: 'Dict' Object Is Not Callable"

I suspect somewhere along the way, the built-in dict() got set to something else (probably like below), and is no longer the object you think it is. d = dict() # new dictionary print(d) ...
phloem's user avatar
  • 4,698
3 votes

Using ArcPy to check for duplicates AND Latest date?

As mentioned in the comments there are other ways of doing this. But you are correct that a Dictionary can be used. Execute in Python window of ArcMap or if standalone you need to create the feature ...
Bera's user avatar
  • 77.8k
3 votes

Converting full road names to abbreviation using ArcGIS field calculator?

If you only want to use the abbreviations for labelling then you could investigate Using an abbreviation dictionary and the About abbreviating and truncating words page: Abbreviation dictionaries ...
PolyGeo's user avatar
  • 65.4k
3 votes
Accepted

Identifying small polygons that are inside larger bounding polygons within same feature class using ArcPy?

Here is the code snippet that finally solved my problem: with arcpy.da.SearchCursor(parcels, ['SHAPE@', 'PARCEL_ID']) as sCurs1: for inside in sCurs1: #small parcels' loop xmax1 = inside[...
jesnes's user avatar
  • 463
3 votes
Accepted

Using a dictionary to find unique values from one field, populate second field with consecutive integers

For a start ID is irrelevant in your fields, you only need the two fields being worked on 'Name', 'Id_2'. You've also set up a search then an update, you don't need to do this, a single update is ...
Michael Stimson's user avatar
3 votes
Accepted

How do I turn dictionary object values into columns for .csv export?

assign properties to a variable (dict) and append the importance dict. The method combine of ee.Classifier returns a dictionary, not a list (as you're casting it to) (disclaimer: I didn't try this ...
Rodrigo E. Principe's user avatar
2 votes

Update field's rows with a value of dictionary

I dont understand all of your code but i'll give some parts of it a try: Why do you need a searchcursor inside the updatecursor when you already have created a Dictionary? You should also need both ...
Bera's user avatar
  • 77.8k
2 votes
Accepted

Issue utilizing Keys:Values from Dict with da.UpdateCursor

I believe you're trying to do something like this: dict= {'xyz':'abc','ghi':'lmno'} for key in dict.iterkeys(): print key + "=" + dict[key] with arcpy.da.UpdateCursor(item,key) as cursor: ...
Michael Stimson's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible