import mercantile
from shapely.geometry import box
import pandas as pd
from cartoframes import CartoDataFrame


class QuadGrid():

    def polyfill(self, input_gdf, zoom_level):

        dfs = []

        for _, row in input_gdf.iterrows():
            bounds = row['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')

@@ -11,16 +11,37 @@
 cantile%0A
+import pandas as pd%0A%0A
 from sha
@@ -69,44 +69,34 @@
 box%0A
-import pandas as pd%0Afrom 
+from ..core.
 carto
+data
 frame
-s
  imp
@@ -181,16 +181,182 @@
 _level):
+%0A        if not hasattr(input_gdf, 'geometry'):%0A            raise ValueError('This dataframe has no valid geometry.')%0A%0A        geometry_name = input_gdf.geometry.name
 %0A%0A      
@@ -366,17 +366,16 @@
 fs = %5B%5D%0A
-%0A
         
@@ -378,17 +378,21 @@
     for 
-_
+index
 , row in
@@ -414,16 +414,71 @@
 rows():%0A
+            input_geometry = input_gdf.geometry%5Bindex%5D%0A
         
@@ -490,29 +490,30 @@
 ounds = 
-row%5B'
+input_
 geometry
 '%5D.bound
@@ -504,18 +504,16 @@
 geometry
-'%5D
 .bounds%0A
@@ -617,20 +617,24 @@
         
-resp
+new_rows
  = %5B%5D%0A  
@@ -648,16 +648,19 @@
    for t
+ile
  in tile
@@ -678,17 +678,23 @@
         
-r
+new_row
  = row.c
@@ -707,32 +707,36 @@
                 
+new_
 geometry = box(*
@@ -754,16 +754,19 @@
 bounds(t
+ile
 ))%0A     
@@ -779,16 +779,20 @@
      if 
+new_
 geometry
@@ -807,21 +807,22 @@
 cts(
-row%5B'
+input_
 geometry
 '%5D):
@@ -817,18 +817,16 @@
 geometry
-'%5D
 ):%0A     
@@ -844,24 +844,37 @@
     
-r%5B'geometry'%5D = 
+new_row%5Bgeometry_name%5D = new_
 geom
@@ -898,17 +898,23 @@
         
-r
+new_row
 %5B'quadke
@@ -939,16 +939,19 @@
 uadkey(t
+ile
 )%0A      
@@ -968,23 +968,32 @@
     
-resp.append(r)%0A
+new_rows.append(new_row)
 %0A   
@@ -1029,12 +1029,16 @@
 ame(
-resp
+new_rows
 ))%0A%0A
@@ -1049,30 +1049,13 @@
     
-return CartoDataFrame(
+df = 
 pd.c
@@ -1087,16 +1087,74 @@
 op=True)
+%0A%0A        return CartoDataFrame(df, geometry=geometry_name
 , crs='e

Refactor QuadGrid class