-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1338 from CartoDB/fix-quad-grid
Fix QuadGrid class
- Loading branch information
Showing
4 changed files
with
28 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
import mercantile | ||
from shapely.geometry import box | ||
import pandas as pd | ||
from cartoframes import CartoDataFrame | ||
|
||
from shapely.geometry import box | ||
from ..core.cartodataframe import CartoDataFrame | ||
|
||
|
||
class QuadGrid(): | ||
|
||
def polyfill(self, input_gdf, zoom_level): | ||
if not hasattr(input_gdf, 'geometry'): | ||
raise ValueError('This dataframe has no valid geometry.') | ||
|
||
dfs = [] | ||
geometry_name = input_gdf.geometry.name | ||
|
||
dfs = [] | ||
for _, row in input_gdf.iterrows(): | ||
bounds = row['geometry'].bounds | ||
input_geometry = row[geometry_name] | ||
bounds = input_geometry.bounds | ||
tiles = mercantile.tiles(bounds[0], bounds[1], bounds[2], bounds[3], zoom_level) | ||
resp = [] | ||
for t in tiles: | ||
r = row.copy() | ||
geometry = box(*mercantile.bounds(t)) | ||
if geometry.intersects(row['geometry']): | ||
r['geometry'] = geometry | ||
r['quadkey'] = mercantile.quadkey(t) | ||
resp.append(r) | ||
|
||
dfs.append(pd.DataFrame(resp)) | ||
|
||
return CartoDataFrame(pd.concat(dfs).reset_index(drop=True), crs='epsg:4326') | ||
new_rows = [] | ||
for tile in tiles: | ||
new_row = row.copy() | ||
new_geometry = box(*mercantile.bounds(tile)) | ||
if new_geometry.intersects(input_geometry): | ||
new_row[geometry_name] = new_geometry | ||
new_row['quadkey'] = mercantile.quadkey(tile) | ||
new_rows.append(new_row) | ||
dfs.append(pd.DataFrame(new_rows)) | ||
|
||
df = pd.concat(dfs).reset_index(drop=True) | ||
|
||
return CartoDataFrame(df, geometry=geometry_name, crs='epsg:4326') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters