Hello,
I'm attempting to add a layer to a map in Jupyter Notebook but am receiving the error:
UnboundLocalError: local variable 'fc_layer_definition' referenced before assignment
My code is as follows:
from arcgis.gis import *
import pandas as pd
gis = GIS()
df = pd.read_csv('file.csv')
df_layer = gis.content.import_data(df)
m = gis.map()
m.add_layer(df_layer)
My dataframe is as follows:
ID ID2 NAME CODE2 LONGITUDE_MEASURE LATITUDE_MEASURE
2 | 2100519 | XXXX | 2100519-001 | -122.960000 | 38.250000 |
---|---|---|---|---|---|
3 | 2100519 | XXX | 2100519-002 | -122.950000 | 38.250000 |
4 | 2100519 | XXXXX | 2100519-004 | -122.957714 | 38.257917 |
I do not understand what variable is being referenced before the assignment. Thank you
Solved! Go to Solution.
You have run into a bug. Looking at the _types.py file, one can see your layer falls through the cracks of conditional statements in the add_layer method. If you just want the error message to go away, insert the following in between Lines 06 and 07 of your original code:
df_layer.layer.layers = [dict(df_layer.layer)]
As as soon Esri fixes the bug, you will want to remove this line because it may either generate an error or overwrite valid information created upstream.
version? I can't get past line 4 without your code throwing an error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-501177b7d1ca> in <module>()
----> 1 df = pd.from_csv('file.csv')
NameError: name 'pd' is not defined
My version info
You are using Jupyter notebook.
The version of the notebook server is: 5.2.2
The server is running on this version of Python:
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 12:30:02) [MSC v.1900 64 bit (AMD64)]
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 12:30:02) [MSC v.1900 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
Oh, well I forgot to include
import pandas as pd
In my code sample. I'm using version 3.6.2
How are you getting Line 05 to work? The core Pandas module has no method from_csv(), that is in the DataFrame object. There is a read_csv() you can use.
After sorting out loading data from the CSV file, the next issue you will run into is Line 06. Although Line 06 will work, I don't think it returns the object you are expecting/wanting. The import_data - arcgis.gis module — arcgis 1.3.0 documentation states:
Imports a Pandas data frame (that has an address column), or an arcgis spatial dataframe into the GIS.
You don't have any address columns, you have longitude and latitude columns. The object returned from import_data() will simply be a table, not a spatial layer. I don't know if that is causing the error, but it is helpful to include the whole Traceback when posting errors:
UnboundLocalError Traceback (most recent call last)
<ipython-input-16-f3eda7fe283f> in <module>()
----> 1 m.add_layer(df_layer)
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis-1.3.0-py3.5.egg\arcgis\widgets\__init__.py in add_layer(self, item, options)
320 options['extent'] = self.extent
321
--> 322 self._webmap.add_layer(item, options)
323 # add to widget's layer list
324 self._widget_layer_list.append(item)
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis-1.3.0-py3.5.egg\arcgis\mapping\_types.py in add_layer(self, layer, options)
286 fc_feature_set = dict(layer.properties.featureSet)
287
--> 288 if 'title' not in fc_layer_definition:
289 fc_layer_definition['title'] = title
290
UnboundLocalError: local variable 'fc_layer_definition' referenced before assignment
Hi,
Thank you -- I made a lot of errors retyping my code.
I was able to add the data to the map (see image) after fixing my previous errors and renaming the lat/long fields to y,x.
I am still recieving the following error even though the data points are on the map:
UnboundLocalError Traceback (most recent call last)
<ipython-input-124-931587bde6f1> in <module>()
----> 1 m.add_layer(wells)
~/anaconda3/lib/python3.6/site-packages/arcgis/widgets/__init__.py in add_layer(self, item, options)
320 options['extent'] = self.extent
321
--> 322 self._webmap.add_layer(item, options)
323 # add to widget's layer list
324 self._widget_layer_list.append(item)
~/anaconda3/lib/python3.6/site-packages/arcgis/mapping/_types.py in add_layer(self, layer, options)
286 fc_feature_set = dict(layer.properties.featureSet)
287
--> 288 if 'title' not in fc_layer_definition:
289 fc_layer_definition['title'] = title
290
UnboundLocalError: local variable 'fc_layer_definition' referenced before assignment
You have run into a bug. Looking at the _types.py file, one can see your layer falls through the cracks of conditional statements in the add_layer method. If you just want the error message to go away, insert the following in between Lines 06 and 07 of your original code:
df_layer.layer.layers = [dict(df_layer.layer)]
As as soon Esri fixes the bug, you will want to remove this line because it may either generate an error or overwrite valid information created upstream.
Thank you for your help. This brings up another question, is the X,Y data being displayed only locally or does the GIS() object temporarily host the layer in ArcGIS online? I did not sign into GIS().
Thank you.
Honestly, I am not even sure that using "x" and "y" columns the way you have is supposed to work, such a workflow surely isn't documented:
https://community.esri.com/thread/207926-importdata-documentation-or-code-bug
Interesting, I thought the XY data would be considered the address, even though it isn't explicitly mentioned in the documentation that XY can be used as the address.
Regardless, thanks for your help.