Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

local variable 'tile_ids' referenced before assignment in calling to_flowdataframe(tessellation=tessellation,self_loops=False) #269

Open
adhamenaya opened this issue Aug 22, 2023 · 0 comments

Comments

@adhamenaya
Copy link

Package version
scikit-mobility: 1.5.3
pandas: 1.3.1
geopandas: 0.10.2
python: 3.8.17

I created a custom tessellation object and I passed it to the to_flowdataframe as follows:
fdf = tdf.to_flowdataframe(tessellation=tessellation,self_loops=False)
This is how I created the tessellation object:

import geopandas as gpd
import h3

# Load a spatial tessellation using H3 hexagons
# Load a spatial tessellation using H3 hexagons
def generate_h3_tessellation(resolution, geo_json_polygon):
    tessellation = []
    tile_ids2 = h3.polyfill(geo_json_polygon, resolution)
    i = 0
    for hex_id in tile_ids2:
        hex_boundary = h3.h3_to_geo_boundary(hex_id, geo_json=True)
        polygon_str = ", ".join([f"{lon} {lat}" for lat, lon in hex_boundary])
        tessellation.append({
            'tile_ID': i,
            'geometry': f'POLYGON (({polygon_str}))'
        })
        i+=1
    return gpd.GeoDataFrame(tessellation, columns=['tile_ID', 'geometry'])


h3_resolution = 7  # Choose a suitable resolution for your analysis

# Define the coordinates for the London bounding box
london_bbox = [
    [-0.510375, 51.286760],  # Bottom-left corner (longitude, latitude)
    [0.334015, 51.691874]    # Top-right corner (longitude, latitude)
]

# Create a Polygon GeoJSON feature for the London bounding box
geo_json_polygon = {
    "type": "Polygon",
    "coordinates": [[
        [london_bbox[0][0], london_bbox[0][1]],
        [london_bbox[0][0], london_bbox[1][1]],
        [london_bbox[1][0], london_bbox[1][1]],
        [london_bbox[1][0], london_bbox[0][1]],
        [london_bbox[0][0], london_bbox[0][1]]
    ]]
}

tessellation = generate_h3_tessellation(h3_resolution, geo_json_polygon)

but I got this error:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[7], line 1
----> 1 fdf = tdf.to_flowdataframe(tessellation=tessellation,self_loops=False)

File ~/anaconda3/envs/scikit-mobility/lib/python3.8/site-packages/skmob/core/trajectorydataframe.py:244, in TrajDataFrame.to_flowdataframe(self, tessellation, self_loops)
    241 self.sort_values(by=self.__operate_on(), ascending=True, inplace=True)
    243 # Step 2: map the trajectory onto the tessellation
--> 244 flow = self.mapping(tessellation, remove_na=False)
    246 # Step 3: groupby tile_id and sum to obtain the flow
    247 flow.loc[:, constants.DESTINATION] = flow[constants.TILE_ID].shift(-1)

File ~/anaconda3/envs/scikit-mobility/lib/python3.8/site-packages/skmob/core/trajectorydataframe.py:329, in TrajDataFrame.mapping(self, tessellation, remove_na)
    326     tile_ids = utils.nearest(gdf, tessellation, constants.TILE_ID)
    328 new_data = self._constructor(self).__finalize__(self)
--> 329 new_data = new_data.merge(tile_ids, right_index=True, left_index=True)
    331 return new_data

UnboundLocalError: local variable 'tile_ids' referenced before assignment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant