<jupyter_start><jupyter_text>Requirments: - pip install pyquadkey2Resources: - Description of Quadkeys https://docs.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system - Library to work with lat lon and QuadKeys https://github.com/CartoDB/python-quadkey - Methodlogy paper https://arxiv.org/ftp/arxiv/papers/2104/2104.07761.pdf - OSM page for getting the tiles based on coordinate system https://wiki.openstreetmap.org/wiki/Slippy_map_tilenamesLon..2Flat._to_tile_numbers_2- Python library documentation https://docs.muetsch.io/pyquadkey2/ - The RWI data is available here: https://data.humdata.org/dataset/relative-wealth-index choose the data that includes the Quadkeys. the data will be downloaded as bulk and includes all csv's for the RWI data.QuadKeys: - the length of the quad key is the level of detail it refers to (longest = more granular level) - parent -child relationship: tile 2 is the perant of tile 20 Our data is in zoom level 14, which is 9.555 meters/pixel resolution Workflow: - Use FB data to populate the list of Quadkeys (QK)- Use the pyquadkey2 library to get four cooordinates for each QK: SW, NE, SE, NW - Use each for cooridnate set to create the tile data - Tranform into polygons, merge back with the FB data: this will result in a polygon type shapefile, each row represents one QK for a point - Export data  * 2.4Km2 is aproximatly 5.76 km2 area * looking into the QuadKeys it seems as though the nearest tile size is 5982842.156102487 Square meteres* this measurment is closest to tiles sized 2.447 KM (length and width)* Coordinate system is WGS 84 datum. * FB data uses level 14 data, each Quadkey is reprsented by 14 digits   Approach: Use data from FB which already includes QuadKeys  Plug-in FB data + AOI<jupyter_code># AOI Kolkata
aoi = gpd.read_file('admin/AOI_Kolkata_Metropolitan_Area_4326.shp')
# fb data for India 
FB_QKdata = pd.read_csv('data/IND_PAK_relative_wealth_index.csv')
FB_QKdata.head()
# change quadkey format to str
FB_QKdata["quadkey1"] = FB_QKdata["quadkey"].astype('str')
FB_QKdata.dtypes
# fill 13 digit quadkeys with 0 before the QK
FB_QKdata['quadkey1'] = FB_QKdata['quadkey1'].apply(lambda x: x.zfill(14))
# take column with quadkeys and transform it to list and then to QuadKey object
fb_qk = FB_QKdata["quadkey1"].tolist()
FB_QK = [QuadKey(i) for i in fb_qk]
# locate the four corners of QuadKey tiles
SW = []
NE = []
SE = []
NW = []

for i in FB_QK:
    #south west point
    sw = i.to_geo(anchor=TileAnchor.ANCHOR_SW)
    SW.append(sw) 
    #north west point
    ne = i.to_geo(anchor=TileAnchor.ANCHOR_NE)
    NE.append(ne) 
    #south east point 
    se = i.to_geo(anchor=TileAnchor.ANCHOR_SE)
    SE.append(se)
    #north west point 
    nw = i.to_geo(anchor=TileAnchor.ANCHOR_NW)
    NW.append(nw)
# check intermediate output
SW[5]
# flip lat and lon for tile corner coordinates
swFinal = [(i[1], i[0]) for i in SW]
neFinal = [(i[1], i[0]) for i in NE]
seFinal = [(i[1], i[0]) for i in SE]
nwFinal = [(i[1], i[0]) for i in NW]
# check intermediate output
print(nwFinal[1:3])
print(seFinal[1:3])
print(neFinal[1:3])
print(swFinal[1:3])
# check the lat and lon are indeed flipped
SW[1]
# generate Polygon object based on four corner coordinates
tiledata = [Polygon([nw, sw, se, ne]) for sw, ne, se, nw in zip(swFinal, neFinal, seFinal, nwFinal)]
# create GeoDataFrame with Polygons
crs = ("epsg:4326")
gdf = gpd.GeoDataFrame(geometry = tiledata, crs = crs)
# make sure aoi crs is the same
aoi.crs
# plot country and aoi
base = gdf.plot(color = 'red', figsize = (20, 15), alpha = 0.3)
aoi.plot(ax = base, color = 'blue')
# add QuadKeys to gdf
gdf['quadkey'] = FB_QK 
gdf
FB_QKdata
gdf.dtypes
# change type of quadkey column to str
gdf["quadkey"] = gdf["quadkey"].astype('str')
# merge gdf and fb data
gdf = gdf.merge(FB_QKdata, left_on = 'quadkey', right_on = 'quadkey1', how = 'inner')
gdf[3003:3006]
# plot choropleth map of RWI
gdf.plot(column = 'rwi', cmap = 'PiYG', figsize = (20, 15), legend = True)
aoi.plot()
# export to shapefile
gdf.to_file("output/FB_QK_data_Kolkata.shp")
# produce summary stats
gdf.describe()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10804263565891473, 'fraction_numerical': 0.027616279069767442, 'mean_word_length': 4.520053475935829, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '361327', 'license': 'MIT', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1451, 'n_words': 496, 'path': 'relative_wealth_index/.ipynb_checkpoints/BingTilesQKversion-checkpoint.ipynb', 'repo_name': 'worldbank/CityScan', 'size': '4128'}