Skip to content

Commit

Permalink
Remove unwanted cells
Browse files Browse the repository at this point in the history
  • Loading branch information
SurajBaloni committed Dec 12, 2024
1 parent 3390b5d commit 60dda12
Showing 1 changed file with 1 addition and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1228,142 +1228,6 @@
"results.head()"
]
},
{
"cell_type": "markdown",
"id": "94260da6-97b3-40fa-bfc5-4d4985c7b56e",
"metadata": {},
"source": [
"## Publishing the results as a feature layer\n",
"\n",
"The code below geocodes the extracted address and publishes the results as a feature layer."
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "d13621d3-a558-4aff-94bc-0fd18c07b1fa",
"metadata": {},
"outputs": [],
"source": [
"# This function generates x,y coordinates based on the extracted location from the model.\n",
"\n",
"def geocode_locations(processed_df, city, region, address_col):\n",
" #creating address with city and region\n",
" add_miner = processed_df[address_col].apply(lambda x: x+f', {city} '+f', {region}') \n",
" chunk_size = 200\n",
" chunks = len(processed_df[address_col])//chunk_size+1\n",
" batch = list()\n",
" for i in range(chunks):\n",
" batch.extend(batch_geocode(list(add_miner.iloc[chunk_size*i:chunk_size*(i+1)])))\n",
" batch_geo_codes = []\n",
" for i,item in enumerate(batch):\n",
" if isinstance(item,dict):\n",
" batch_geo_codes.append(item['location']) \n",
" else:\n",
" batch_geo_codes.append('') \n",
" processed_df['geo_codes'] = batch_geo_codes \n",
" return processed_df"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "4748981f-9284-43c6-a097-ff26a34a8e1d",
"metadata": {},
"outputs": [],
"source": [
"#This function converts the dataframe to a spatailly enabled dataframe.\n",
"\n",
"def prepare_sdf(processed_df):\n",
" processed_df['geo_codes_x'] = 'x'\n",
" processed_df['geo_codes_y'] = 'y'\n",
" for i,geo_code in processed_df['geo_codes'].items():\n",
" if geo_code == '': \n",
" processed_df.drop(i, inplace=True) #dropping rows with empty location\n",
" else:\n",
" processed_df['geo_codes_x'].loc[i] = geo_code.get('x')\n",
" processed_df['geo_codes_y'].loc[i] = geo_code.get('y')\n",
" \n",
" sdf = processed_df.reset_index(drop=True)\n",
" sdf['geo_x_y'] = sdf['geo_codes_x'].astype('str') + ',' +sdf['geo_codes_y'].astype('str')\n",
" sdf = pd.DataFrame.spatial.from_df(sdf, address_column='geo_x_y') #adding geometry to the dataframe\n",
" sdf.drop(['geo_codes_x','geo_codes_y','geo_x_y','geo_codes'], axis=1, inplace=True) #dropping redundant columns\n",
" return sdf"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "0dd499fe-6972-4215-a05a-15a09f130f14",
"metadata": {},
"outputs": [],
"source": [
"#This function will publish the spatical dataframe as a feature layer.\n",
"\n",
"def publish_to_feature(df, gis, layer_title:str, tags:str, city:str, \n",
" region:str, address_col:str):\n",
" processed_df = geocode_locations(df, city, region, address_col)\n",
" sdf = prepare_sdf(processed_df)\n",
" try: \n",
" layer = sdf.spatial.to_featurelayer(layer_title, gis, tags) \n",
" except:\n",
" layer = sdf.spatial.to_featurelayer(layer_title, gis, tags)\n",
"\n",
" return layer "
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "fc06e50f-b297-42d8-a03c-8b23225d9e3a",
"metadata": {},
"outputs": [],
"source": [
"# This will take few minutes to run\n",
"cheshire_fire_layer = publish_to_feature(results, gis, layer_title='Cheshire-fire' + str(datetime.datetime.now().microsecond), \n",
" tags='nlp, cheshire, fire', city='Cheshire', \n",
" region='England', address_col='address')"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "b8a7b088-464f-44d7-9269-f71592c68603",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
" <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
" <a href='https://ivt.maps.arcgis.com//home/item.html?id=4ad9a4e00b7e431a9fbbffa78fbc5afa' target='_blank'>\n",
" <img src='http://static.arcgis.com/images/desktopapp.png' class=\"itemThumbnail\">\n",
" </a>\n",
" </div>\n",
"\n",
" <div class=\"item_right\" style=\"float: none; width: auto; overflow: hidden;\">\n",
" <a href='https://ivt.maps.arcgis.com//home/item.html?id=4ad9a4e00b7e431a9fbbffa78fbc5afa' target='_blank'><b>Cheshire-fire855240</b>\n",
" </a>\n",
" <br/><br/><img src='https://ivt.maps.arcgis.com//home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by delhidev_ivt\n",
" <br/>Last Modified: October 16, 2024\n",
" <br/>0 comments, 0 views\n",
" </div>\n",
" </div>\n",
" "
],
"text/plain": [
"<Item title:\"Cheshire-fire855240\" type:Feature Layer Collection owner:delhidev_ivt>"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cheshire_fire_layer"
]
},
{
"cell_type": "markdown",
"id": "7360d29f-de9d-499f-b836-40d2d14f0eaf",
Expand All @@ -1387,7 +1251,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:conda-dl_10_Sept_1]",
"display_name": "Python [conda env:conda-dl_10_Sept_1] *",
"language": "python",
"name": "conda-env-conda-dl_10_Sept_1-py"
},
Expand Down

0 comments on commit 60dda12

Please sign in to comment.