Skip to content

Commit

Permalink
add markdown and new clean notebook for the script
Browse files Browse the repository at this point in the history
  • Loading branch information
natam1 committed Sep 6, 2023
1 parent 1d6e917 commit d683ac5
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 19 deletions.
10 changes: 10 additions & 0 deletions project_prioritization/accessibility/Markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Converting Jsons to Shapefiles for Accessibility Analysis

Use these steps to convert jsons to a zipped shape file:
1. Upload json file to folder in Jupyter Notebook
2. Open notebook `project_location_to_shapefiles.ipynb`
3. Add the following information for the uploaded to the function `json_to_shpfile`:
* json file,
* name for geojson
* then the name of the zip file output
4. Download your zipped shape file from the Cal-ITP GCS bucket or from the folder [zipped_locations](data-analyses/project_prioritization/accessibility/zipped_locations).
17 changes: 12 additions & 5 deletions project_prioritization/accessibility/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@

from shared_utils import utils

# local_path = "/home/jovyan/data-analyses/project_prioritization/accessibility/"
local_folder = "zipped_locations/"
GCS_PATH = "gs://calitp-analytics-data/data-analyses/project_prioritization/zipped_shpfiles/"

"""
For this function you will need to specify the name of the geojson file you want to read in before. example:
file = "project_location_bike.geojson"
"""

def read_and_create_shpfiles(geojson_file, zip_name):
def read_and_create_shpfiles(geojson_file, zip_name, localfolder):
localfolder_str = str(localfolder)
location = gpd.read_file(geojson_file)
location_zipped = utils.make_zipped_shapefile(location, zip_name, gcs_folder = GCS_PATH)
location_zipped = utils.make_zipped_shapefile(location, local_path = f"{localfolder_str}{zip_name}", gcs_folder = GCS_PATH)


## Function takes a json, creates a string and then modifies it so that it is in a geojson format that works for us.
Expand Down Expand Up @@ -60,10 +61,16 @@ def manipulate_json(json_file, geojson_file_name):


## function puts together the two previous functions, to get from json file to shp file.
def json_to_shpfile(json_file, geojson_file_name, zip_name):
def json_to_shpfile(json_file, geojson_file_name, zip_name, local_folder):
'''
use this function to convert jsons to a zipped shp file
1. upload json file to folder in jupyter notebook
2. open notebook project_location_to_shapefiles.ipynb
3. add information for the uploaded json file, name for geojson and then the name of the zip file output
'''
## run through json manipulation
manipulate_json(json_file, geojson_file_name)

## run through function to get shpfiles
read_and_create_shpfiles(f"{geojson_file_name}.geojson", zip_name)
read_and_create_shpfiles(f"{geojson_file_name}.geojson", zip_name, local_folder)

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "20aaf3ca-9186-47ab-bb5a-2779c43de265",
"metadata": {},
"source": [
"# Converting Jsons to Zipped GeoJsons"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6fcea931-86eb-4cc5-947a-3444e71485a7",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import geopandas as gpd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import _utils\n",
"\n",
"import json\n",
"import re \n",
"import ast\n",
"import gcsfs\n",
"import os"
]
},
{
"cell_type": "markdown",
"id": "17f7db63-e65e-4821-a467-5a4628c998be",
"metadata": {},
"source": [
"### Use function in _utils"
]
},
{
"cell_type": "markdown",
"id": "e011563b-e4d8-4738-90f6-761499accfd3",
"metadata": {},
"source": [
"Add the following information for the uploaded to the function `json_to_shpfile`:\n",
"* json file, \n",
"* name for geojson \n",
"* then the name of the zip file output \n",
"\n",
"\n",
"Download your zipped shape file from the Cal-ITP GCS bucket or from the folder [zipped_locations](data-analyses/project_prioritization/accessibility/zipped_locations). \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05afd750-4490-4485-b2e4-14c46047de2b",
"metadata": {},
"outputs": [],
"source": [
"_utils.json_to_shpfile('.json', ##json file upload here\n",
" '' ##local file name\n",
" 'test_zipped_us101cc' ###name of the final zip file\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "34e1d063-d483-4f38-a702-5fd4130f8930",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3ca5cc4-2529-4f1a-87b6-d28a3282a51b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit d683ac5

Please sign in to comment.