A Python API Access Client for the First Street Foundation API
Project description
First Street Foundation API Access Documentation (Python 3.6+)
The First Street Foundation API Access (Python) is a wrapper used to bulk extract flood data from the First Street Foundation API
Current Release:
Current release: 0.1.1
Notice: This API wrapper is subject to change.
Table of contents
Installation
NOTE: This project requires Python 3.6+ to run.
-
Go to the Python page (https://www.python.org/downloads/) and download then install Python version 3. Make sure that the checkbox is checked for Python to be added to the PATH
-
Install the source code manager Git
-
[Optional] Open a new
powershell console
/bash terminal
or close and re-open a console and create and activate a new virtual environment in the project directory:python -m venv /path/to/new/virtual/environment cd /path/to/project .\venv\Scripts\activate
-
Use pip to install the project
cd /path/to/project pip install fsf-api-access-python
-
The project is now setup and can be ran through one of the two methods below. See
Products
for additional details on how to extract flood data products from the API.Example File Structure: . ├── fsf_api_access_python. ├── venv ├── my_script.py
Running the Project - Method 1: Through the Command Line
[Reminder] Keep your API key safe, and do not share it with others!
[NOTE] This method will always generate a CSV.
-
MacOS or Linux
[Required] Open a
bash terminal
and set an Environmental Variable with thevariable_name
asFSF_API_KEY
and thevariable_value
with theAPI_KEY
with the commandexport FSF_API_KEY=your personal API key
Windows
[Required] Set an Environmental Variable with the
variable_name
asFSF_API_KEY
and thevariable_value
with theAPI_KEY
. -
Open/Re-open a
powershell console
/bash terminal
and navigate to the project directory. Next, call one of the methods described below in theProducts
section through the command line. See theExamples
section for more examples.cd /path/to/project python -m firststreet -p <product>.<product_subtype> -i <fsids> -f <file_name> -l <lookup_type>
Command Line Argument Details:
-
-p/--product PRODUCT
: [REQUIRED] The product to call from the APIExample:
-p location.get_detail
-
[-api_key/--api_key API_KEY]
: [OPTIONAL] If step 1 is skipped or unavailable, this argument can take theAPI_KEY
instead.Example:
-a XXXXXXXXXXXXXXXXXXX
-
[-v/--version VERSION]
: [OPTIONAL] The version of the API to call. Defaults to the current version.Example:
-v v1
-
[-i/--fsids FSIDS]
: [OPTIONAL] The FSIDs to search for with the productExample:
-i 541114211,541229206
-
[-l/--location LOOKUP_TYPE]
: [OPTIONAL] The lookup location type (property, neighborhood, city, zcta, tract, county, cd, state)Example:
-l property
-
[-limit/--limit LIMIT]
: [OPTIONAL] The max number of connections to make the the API at the same time. Defaults to 100 connectionsExample:
-limit 20
-
[-log/--log LOG]
: [OPTIONAL] To log info output to the console or not. Defaults to TrueExample:
-l False
-
[-f/--file FILE]
: [OPTIONAL] A file of FSIDs (one per line) to search for with the productExample:
-f sample.txt
Content of a sample text file. Note that the file must be in the same directory as the project:
541114211 540456284 541229206 540563324 541262690 540651172
Running the Project - Method 2: Through the Client
[Reminder] Keep your API key safe, and do not share it with others!
[NOTE] This method will NOT generate a CSV by default. An argument must be passed to generate a CSV.
-
Create a new Python script (by using notepad or any other text editor) and initialize a First Street Foundation API Client.
# Contents of my_script.py import firststreet fs = firststreet.FirstStreet("api-key")
Client Initialization Details
firststreet.FirstStreet(api_key, [version], [log])
Arguments
- api_key
string
: The assigned API key to access the API. - version
string= v1
: The version of the API to access. Defaults to the current version. - log
bool= True
: Setting for whether to log info or not. Defaults to True.
- api_key
-
Call one of the methods described below in the
Products
section with the required arguments. See theExamples
section for more examples.fs.<product>.<product_subtype>(<fsids: list>, <lookup_type: string>, <csv: boolean>)
OR
-
Run the python script.
Products
NOTE: As of the current release, only direct FSID lookups are available through this wrapper. Lat/lng and address query will be in a later release
Location
The Location API provides Detail
and Summary
data for the given FSIDs.
location.<method>
get_detail
(fsidslist
, location_typestring
, csvbool
, [core_limitint=100
]) - Returns an array ofLocation Detail
product for the given IDs, location_type, and optionally creates a csv fileget_summary
(fsidslist
, location_typestring
, csvbool
, [core_limitint=100
]) - Returns an array ofLocation Summary
product for the given IDs, location_type, and optionally creates a csv file
Probability
The Probability API provides Depth
, Chance
, Cumulative
, Count
data for the given FSIDs.
probability.<method>
get_depth
(fsidslist
, csvbool
, [core_limitint=100
]) - Returns an array ofProbability Depth
product for the given IDs, and optionally creates a csv fileget_chance
(fsidslist
, csvbool
, [core_limitint=100
]) - Returns an array ofProbability Chance
product for the given IDs, and optionally creates a csv fileget_count_summary
(fsidslist
, csvbool
, [core_limitint=100
]) - Returns an array ofProbability Count-Summary
product for the given IDs, and optionally creates a csv fileget_cumulative
(fsidslist
, csvbool
, [core_limitint=100
]) - Returns an array ofProbability Cumulative
product for the given IDs, and optionally creates a csv fileget_count
(fsidslist
, location_typestring
, csvbool
, [core_limitint=100
]) - Returns an array ofProbability Count
product for the given IDs, location_type, and optionally creates a csv file
Historic
The Historic API provides Summary
and Event
data for the given FSIDs.
historic.<method>
get_event
(fsidslist
, csvbool
, [core_limitint=100
]) - Returns an array ofHistoric Event
product for the given historic IDs, and optionally creates a csv fileget_summary
(fsidslist
, location_typestring
, csvbool
, [core_limitint=100
]) - Returns an array ofHistoric Summary
product for the given IDs, location_type, and optionally creates a csv file
Adaptation
The Adaptation API provides Summary
and Project
data for the given FSIDs.
adaptation.<method>
get_project
(fsidslist
, csvbool
, [core_limitint=100
]) - Returns an array ofAdaptation Project
product for the given adaptation IDs, and optionally creates a csv fileget_summary
(fsidslist
, location_typestring
, csvbool
, [core_limitint=100
]) - Returns an array ofAdaptation Summary
product for the given IDs, location_type, and optionally creates a csv file
Fema
The Fema API provides NFIP
data for the given FSIDs.
fema.<method>
get_nfip
(fsidslist
, location_typestring
, csvbool
, [core_limitint=100
]) - Returns an array ofFema NFIP
product for the given IDs, location_type, and optionally creates a csv file
Environmental
The Environmental API provides Precipitation
data for the given FSIDs.
environmental.<method>
get_precipitation
(fsidslist
, csvbool
, [core_limitint=100
]) - Returns an array ofEnvironmental Precipitation
product for the given county IDs, and optionally creates a csv file
Examples
[Reminder] Keep your API key safe, and do not share it with others!
-
Single FSID Extraction Through the Client:
# Contents of sample.py import firststreet fs = firststreet.FirstStreet("api-key") fsids = [39035103400] probability = fs.probability.get_count(fsids, "tract") print(probability[0].fsid) print(probability[0].count)
-
Multiple FSIDs Extraction Through the Client:
# Contents of sample.py import firststreet fs = firststreet.FirstStreet("api-key") fsids = [1912000, 1979140] details = fs.location.get_detail(fsids, "property") print(details[0].fsid) print(details[0].route) print(details[1].fsid)
-
Adaptation detail Extraction to CSV Through the Client:
# Contents of sample.py import firststreet fs = firststreet.FirstStreet("api-key") fs.adaptation.get_detail([29], csv=True)
Output File:
adaptationId,name,type,scenario,conveyance,returnPeriod 29,Riverfront Park,bioswale,fluvial,False,500 29,Riverfront Park,bioswale,pluvial,False,500 29,Riverfront Park,detention basin,fluvial,False,500 29,Riverfront Park,detention basin,pluvial,False,500 29,Riverfront Park,levee,fluvial,False,500 29,Riverfront Park,levee,pluvial,False,500 29,Riverfront Park,pervious pavement,fluvial,False,500 29,Riverfront Park,pervious pavement,pluvial,False,500
-
Multiple FSIDs Extraction Using a File Through the Client:
# Contents of sample.py import firststreet fs = firststreet.FirstStreet("api-key") fsids = [1912000, 1979140] details = fs.location.get_detail(fsids, "property") print(details[0].fsid) print(details[0].route) print(details[1].fsid)
-
Single FSID Extraction to CSV Through the Command Line:
python -m firststreet -p fs.probability.get_depth -i 390000227
-
Multiple FSIDs Extraction to CSV Through the Command Line:
python -m firststreet -p historic.get_summary -i 1912000,1979140 -l property
-
Bulk FSIDs Extraction From File to CSV Through the Command Line:
Content of sample.txt:
541114211 540456284 541229206 540563324 541262690 540651172
python -m firststreet -p location.get_summary -f sample.txt -l property
Output File 2020_06_10_20_33_12_location_summary_property.csv:
fsid,floodFactor,riskDirection,environmentalRisk,historic,adaptation 541114211,1,0,1,0,0 540456284,9,0,1,0,0 541229206,9,1,1,0,0 540563324,1,0,1,0,0 541262690,1,0,1,0,0 540651172,1,0,1,0,0
CSV File Output:
Any product above can be additionally exported to a CSV file for further usage if the csv boolean is set during the product call, or any call using the command line. The extracted can be found in the data_csv
directory of the project folder (if at least one CSV has been extracted).
CSV File Name:
The file name will be in the format of YYYY_MM_DD_HH_mm_SS_product_subtype_lookup.csv
.
Ex:
2020_06_10_17_29_49_adaptation_detail.csv
2020_06_10_17_33_56_adaptation_summary_property.csv
CSV File Content
The contents of the CSV file will follow similar formats as the First Street Foundation API - V1.0 Overview and Dictionary
, but all lists will be expanded to a flat file. For any values that are null or not available, <NA>
will be used.
Ex:
fsid,year,returnPeriod,bin,low,mid,high
7935,2020,20,20,<NA>,2,<NA>
7935,2020,20,50,<NA>,1,<NA>
7935,2020,20,55,<NA>,2,<NA>
7935,2020,20,65,<NA>,2,<NA>
7935,2020,20,75,<NA>,2,<NA>
7935,2020,20,95,<NA>,1,<NA>
7935,2020,20,100,<NA>,1,<NA>
...
Updating the Project to the Newest Version:
If an update is made to this project, you will need to upgrade through PyPi
-
Open a new
powershell console
/bash terminal
, navigate to the project, and run the upgrade command:cd /path/to/project .\venv\Scripts\activate pip install --upgrade fsf_api_access_python
-
The project should now be updated to the newest version
License
MIT License
Copyright (c) 2020 First Street Foundation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fsf-api-access_python-0.1.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1afd7d0ccca9a4ec31b6d7bae0f5a0dcd53b33105698fcf0e77980cf1d5d99d7 |
|
MD5 | aca818078180eb9dc2e4210a00d90672 |
|
BLAKE2b-256 | a1c10be434814765e03e9c9111444ea276f76dc84511dfcc1172f3e089b09876 |
Hashes for fsf_api_access_python-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6044188dce996ace666298a7200cf0490d6541a949bc951b1dd2f8ca3c47a01 |
|
MD5 | 4467f93039b14a84006e7ea1a612c1a0 |
|
BLAKE2b-256 | 3590014ca21eb7e4111a46b90663f868a1d761f36d519b019e556728200e72f3 |