Editing and updating datasets

Because CARTOframes incorporates the Pandas dataframes objects, which can be edited in memory, and writes to the datasets stored in the CARTO account, we can create scripts that will automate the upload of geospatial data. Datasets can be updated entirely, or individual rows and values can be updated using Pandas data methods such as replace. This, coupled with Builder, the CARTO web-map deployment tool, makes it easy to create GIS with a web-map frontend and cloud data storage that can be managed using scripting.

In this example code, the name of the state that contains the NBA arena is found using the intersect query. The names are added to a list, and the list is added to the arena dataframe as a new column called states. The geometry data stored in the arenas dataset are required to be converted into Shapely objects, using the loads module:

import geopandas as gdp
import cartoframes
import pandas as pd
from shapely.wkb import loads
APIKEY = "API KEY"
cc = cartoframes.CartoContext(base_url='https://{username}.carto.com/',
api_key=APIKEY)
arenas_df = cc.read('arenas_nba')
shp = r"C:DataUS_StatesUS_States.shp"
states_df = gdp.read_file(shp)
data = []
for index, ref in arenas_df.iterrows():
check = 0
for index2, orig in states_df.iterrows():
if loads(ref['the_geom'], hex=True).intersects(orig['geometry']):
data.append(orig['STATE'])
check = 1
if check == 0:
data.append(None)
arenas_df['state'] = data
cc.write(arenas_df,'arenas_nba', overwrite=True)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset