UnboundLocalError: local variable 'fc_layer_definition' referenced before assignment

2976
10
Jump to solution
01-13-2018 03:27 PM
JasonCarter
New Contributor III

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

22100519XXXX2100519-001-122.96000038.250000
32100519XXX2100519-002-122.95000038.250000
42100519XXXXX2100519-004-122.95771438.257917

I do not understand what variable is being referenced before the assignment. Thank you

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

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.

View solution in original post

10 Replies
DanPatterson_Retired
MVP Emeritus

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

Server Information:

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

Current Kernel Information:

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.
JasonCarter
New Contributor III

Oh, well I forgot to include 

import pandas as pd

In my code sample. I'm using version 3.6.2

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JasonCarter
New Contributor III

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
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

JasonCarter
New Contributor III

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.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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 

0 Kudos
JasonCarter
New Contributor III

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.

0 Kudos