Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit c39b49a

Browse files
authored
Merge pull request #7 from FirstStreet/develop
v1.2.0 with lat/lng and address functionality, and bug fixes
2 parents 6ec5106 + a1976ec commit c39b49a

File tree

12 files changed

+126
-100
lines changed

12 files changed

+126
-100
lines changed

README.md

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The First Street Foundation API Access (Python) is a wrapper used to bulk extrac
1414
* [Running the Project - Method 2: Through the Client](#method2)
1515
- [Client Initialization Details](#client-init)
1616
- [Arguments](#client-arguments)
17+
- [If you are using Jupyter Notebook](#jupyter_setup)
1718
- **[Products](#products)**
1819
- [Location](#location)
1920
- [Probability](#probability)
@@ -31,17 +32,12 @@ The First Street Foundation API Access (Python) is a wrapper used to bulk extrac
3132
<a name="installation"></a>
3233
# [Installation](#toc)
3334
**NOTE**: This project requires [Python](https://www.python.org/downloads/) 3.6+ to run.
35+
3436
1. 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**
3537

36-
![Screenshot](https://raw.githubusercontent.com/FirstStreet/fsf_api_access_python/master/doc/images//1.1.1.png)
37-
38-
![Screenshot](https://raw.githubusercontent.com/FirstStreet/fsf_api_access_python/master/doc/images//1.1.2.png)
39-
40-
![Screenshot](https://raw.githubusercontent.com/FirstStreet/fsf_api_access_python/master/doc/images//1.1.3.png)
38+
A more detailed and in-depth guide on how to install Python can be found [here](https://realpython.com/installing-python/)
4139

42-
![Screenshot](https://raw.githubusercontent.com/FirstStreet/fsf_api_access_python/master/doc/images//1.1.4.png)
43-
44-
2. [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:
40+
2. [Optional] Open a new `powershell console` / `bash terminal`and create and activate a new virtual environment in the project directory:
4541
```sh
4642
python -m venv /path/to/new/virtual/environment
4743

@@ -98,7 +94,7 @@ The First Street Foundation API Access (Python) is a wrapper used to bulk extrac
9894
```sh
9995
cd /path/to/project
10096
101-
python -m firststreet -p <product>.<product_subtype> -i <fsids> -f <file_name> -l <lookup_type>
97+
python -m firststreet -p <product>.<product_subtype> -i <search_item> -f <file_name> -l <lookup_type>
10298
```
10399

104100
![Screenshot](https://raw.githubusercontent.com/FirstStreet/fsf_api_access_python/master/doc/images//3.2.1.png)
@@ -118,9 +114,11 @@ The First Street Foundation API Access (Python) is a wrapper used to bulk extrac
118114

119115
Example: ```-v v1```
120116

121-
- `[-i/--fsids FSIDS]`: [OPTIONAL] The FSIDs to search for with the product
117+
- `[-i/--search_items SEARCH_ITEM]`: [OPTIONAL] The Search Items to search for with the product.
118+
119+
**NOTE** THE LIST MUST BE A SEPRATED WITH A SEMICOLON (;) INSTEAD OF A COMMA
122120

123-
Example: ```-i 541114211,541229206```
121+
Example: ```-i 541114211;541229206```
124122

125123
- `[-l/--location LOOKUP_TYPE]`: [OPTIONAL] The lookup location type (property, neighborhood, city, zcta, tract, county, cd, state)
126124

@@ -134,7 +132,7 @@ The First Street Foundation API Access (Python) is a wrapper used to bulk extrac
134132

135133
Example: ```-l False```
136134

137-
- `[-f/--file FILE]`: [OPTIONAL] A file of FSIDs (one per line) to search for with the product
135+
- `[-f/--file FILE]`: [OPTIONAL] A file of Search Items (one per line) to search for with the product
138136

139137
Example: ```-f sample.txt```
140138

@@ -164,6 +162,7 @@ The First Street Foundation API Access (Python) is a wrapper used to bulk extrac
164162
import firststreet
165163
fs = firststreet.FirstStreet("api-key")
166164
```
165+
167166
<a name="client-init"></a>
168167
#### [Client Initialization Details](#toc)
169168

@@ -179,7 +178,7 @@ The First Street Foundation API Access (Python) is a wrapper used to bulk extrac
179178

180179
2. Call one of the methods described below in the `Products` section with the required arguments. See the `Examples` section for more examples.
181180
```python
182-
fs.<product>.<product_subtype>(<fsids: list>, <lookup_type: string>, <csv: boolean>)
181+
fs.<product>.<product_subtype>(<search_items: list>, <lookup_type: string>, <csv: boolean>)
183182
```
184183

185184
![Screenshot](https://raw.githubusercontent.com/FirstStreet/fsf_api_access_python/master/doc/images//2.2.1.png)
@@ -194,15 +193,29 @@ The First Street Foundation API Access (Python) is a wrapper used to bulk extrac
194193

195194
![Screenshot](https://raw.githubusercontent.com/FirstStreet/fsf_api_access_python/master/doc/images//2.3.1.png)
196195

196+
197+
<a name="jupyter_setup"></a>
198+
### [If you are using Jupyter Notebook](#toc)
199+
1. Add the following to the top of the file before the [client initialization](client-init) to allow the jupyter notebook and download loops to work correctly
200+
201+
```python
202+
# Setup For Notebook only
203+
import nest_asyncio
204+
nest_asyncio.apply()
205+
```
206+
197207
<a name="products"></a>
198208
# [Products](#toc)
199209

200210
More information on each product can be found at the [First Street Foundation API Data Dictionary](https://docs.firststreet.dev/docs)
201211

202212
<a name="Search Item"></a>
203-
#### [Search Item](#toc)
213+
#### [Search Item:](#toc)
204214

205-
For every product, a list or file of `search items` must be provided to the product call. There are 3 types of `search items` corresponding to the 3 types of lookups.
215+
For every product, a list or file of `search items` must be provided to the product call.
216+
There are 3 types of `search items` corresponding to the 3 types of lookups.
217+
(More information on the Lookup types can be found on the [Lookups Page on the First Street Foundation API Data Dictionary](https://docs.firststreet.dev/docs/lookups)
218+
)
206219

207220
1. FSID (`int`): The simplest type of lookup is a fsid lookup. If you know the fsid of the specific location, you can navigate directly to the specific product's information using the location's fsid. Example FSID: `18`
208221

@@ -226,8 +239,9 @@ Example file of `search items`:
226239
<a name="location"></a>
227240
#### [Location](#toc)
228241

229-
The Location API provides `Detail` and `Summary` data for the given FSIDs.
230-
242+
The Location API provides `Detail` and `Summary` data for the given SearchItems.
243+
(More information on the Location product can be found on the [Location Page on the First Street Foundation API Data Dictionary](https://docs.firststreet.dev/docs/location-introduction)
244+
)
231245
```python
232246
location.<method>
233247
```
@@ -238,7 +252,9 @@ location.<method>
238252
<a name="probability"></a>
239253
#### [Probability](#toc)
240254

241-
The Probability API provides `Depth`, `Chance`, `Cumulative`, `Count` data for the given FSIDs.
255+
The Probability API provides `Depth`, `Chance`, `Cumulative`, `Count` data for the given SearchItems.
256+
(More information on the Probability product can be found on the [Probability Page on the First Street Foundation API Data Dictionary](https://docs.firststreet.dev/docs/probability-depth)
257+
)
242258

243259
```python
244260
probability.<method>
@@ -253,7 +269,9 @@ probability.<method>
253269
<a name="historic"></a>
254270
#### [Historic](#toc)
255271

256-
The Historic API provides `Summary` and `Event` data for the given FSIDs.
272+
The Historic API provides `Summary` and `Event` data for the given SearchItems.
273+
(More information on the Historic product can be found on the [Historic Page on the First Street Foundation API Data Dictionary](https://docs.firststreet.dev/docs/historic-summary)
274+
)
257275

258276
```python
259277
historic.<method>
@@ -265,8 +283,10 @@ historic.<method>
265283

266284
<a name="adaptation"></a>
267285
#### [Adaptation](#toc)
286+
(More information on the Adaptation product can be found on the [Adaptation Page on the First Street Foundation API Data Dictionary](https://docs.firststreet.dev/docs/adaptation-introduction)
287+
)
268288

269-
The Adaptation API provides `Summary` and `Project` data for the given FSIDs.
289+
The Adaptation API provides `Summary` and `Project` data for the given SearchItems.
270290

271291
```python
272292
adaptation.<method>
@@ -279,7 +299,9 @@ adaptation.<method>
279299
<a name="fema"></a>
280300
#### [Fema](#toc)
281301

282-
The Fema API provides `NFIP` data for the given FSIDs.
302+
The Fema API provides `NFIP` data for the given SearchItems.
303+
(More information on the Fema NFIP product can be found on the [Fema NFIP Page on the First Street Foundation API Data Dictionary](https://docs.firststreet.dev/docs/fema-nfip)
304+
)
283305

284306
```python
285307
fema.<method>
@@ -290,7 +312,9 @@ fema.<method>
290312
<a name="environmental"></a>
291313
#### [Environmental](#toc)
292314

293-
The Environmental API provides `Precipitation` data for the given FSIDs.
315+
The Environmental API provides `Precipitation` data for the given SearchItems.
316+
(More information on the Environmental Precipitation product can be found on the [Environmental Precipitation Page on the First Street Foundation API Data Dictionary](https://docs.firststreet.dev/docs/environmental-precipitation)
317+
)
294318

295319
```python
296320
environmental.<method>
@@ -371,7 +395,7 @@ environmental.<method>
371395

372396
5. Multiple FSIDs Extraction to CSV Through the Command Line:
373397
```sh
374-
python -m firststreet -p historic.get_summary -i 1912000,1979140 -l property
398+
python -m firststreet -p historic.get_summary -i 1912000;1979140 -l property
375399
```
376400

377401
6. Bulk FSIDs Extraction From File to CSV Through the Command Line:

firststreet/__main__.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Standard Imports
55
import argparse
6+
import ast
67
import os
78
import logging
89
from distutils.util import strtobool
@@ -18,28 +19,28 @@
1819
parser.add_argument("-p", "--product", help="Example: adaptation_detail", required=True)
1920
parser.add_argument("-api_key", "--api_key", required=False)
2021
parser.add_argument("-v", "--version", required=False)
21-
parser.add_argument("-i", "--fsids", help="Example: 28,29", required=False,)
22+
parser.add_argument("-i", "--search_items", help="Example: 28,29", required=False,)
2223
parser.add_argument("-l", "--location", help="Example: property", required=False)
2324
parser.add_argument("-limit", "--limit", help="Example: 100", required=False, default="100")
2425
parser.add_argument("-log", "--log", help="Example: False", required=False, default="True")
2526
parser.add_argument("-f", "--file", help="Example: ./sample.txt", required=False)
2627

2728
argument = parser.parse_args()
2829

29-
# Merge FSIDs from file and list input
30-
fsids = []
31-
if argument.fsids:
32-
try:
33-
fsids += list(map(int, argument.fsids.strip().split(",")))
34-
except ValueError:
35-
logging.warning("An invalid fsid list was provided. Please check this input list is a comma-separated "
36-
"list of integers: '{}'".format(argument.fsids))
30+
# Merge search_item from file and list input
31+
search_items = []
32+
if argument.search_items:
33+
for search_item in argument.search_items.strip().split(";"):
34+
try:
35+
search_items.append(ast.literal_eval(search_item))
36+
except (SyntaxError, ValueError):
37+
search_items.append(search_item)
3738

3839
if argument.file:
39-
fsids += read_search_items_from_file(argument.file)
40+
search_items += read_search_items_from_file(argument.file)
4041

41-
# Ensure there is at least a product and FSID
42-
if fsids:
42+
# Ensure there is at least a product and search item
43+
if search_items:
4344

4445
# Try to get the API key either from env var or the parameter
4546
if not argument.api_key:
@@ -57,54 +58,54 @@
5758
limit = int(argument.limit)
5859

5960
if argument.product == 'adaptation.get_detail':
60-
fs.adaptation.get_detail(fsids, csv=True, limit=limit)
61+
fs.adaptation.get_detail(search_items, csv=True, limit=limit)
6162

6263
elif argument.product == 'adaptation.get_summary':
63-
fs.adaptation.get_summary(fsids, argument.location, csv=True, limit=limit)
64+
fs.adaptation.get_summary(search_items, argument.location, csv=True, limit=limit)
6465

6566
elif argument.product == 'adaptation.get_details_by_location':
66-
fs.adaptation.get_details_by_location(fsids, argument.location, csv=True, limit=limit)
67+
fs.adaptation.get_details_by_location(search_items, argument.location, csv=True, limit=limit)
6768

6869
elif argument.product == 'probability.get_depth':
69-
fs.probability.get_depth(fsids, csv=True, limit=limit)
70+
fs.probability.get_depth(search_items, csv=True, limit=limit)
7071

7172
elif argument.product == 'probability.get_chance':
72-
fs.probability.get_chance(fsids, csv=True, limit=limit)
73+
fs.probability.get_chance(search_items, csv=True, limit=limit)
7374

7475
elif argument.product == 'probability.get_count_summary':
75-
fs.probability.get_count_summary(fsids, csv=True, limit=limit)
76+
fs.probability.get_count_summary(search_items, csv=True, limit=limit)
7677

7778
elif argument.product == 'probability.get_cumulative':
78-
fs.probability.get_cumulative(fsids, csv=True, limit=limit)
79+
fs.probability.get_cumulative(search_items, csv=True, limit=limit)
7980

8081
elif argument.product == 'probability.get_count':
81-
fs.probability.get_count(fsids, argument.location, csv=True, limit=limit)
82+
fs.probability.get_count(search_items, argument.location, csv=True, limit=limit)
8283

8384
elif argument.product == 'historic.get_event':
84-
fs.historic.get_event(fsids, csv=True, limit=limit)
85+
fs.historic.get_event(search_items, csv=True, limit=limit)
8586

8687
elif argument.product == 'historic.get_summary':
87-
fs.historic.get_summary(fsids, argument.location, csv=True, limit=limit)
88+
fs.historic.get_summary(search_items, argument.location, csv=True, limit=limit)
8889

8990
elif argument.product == 'historic.get_events_by_location':
90-
fs.historic.get_events_by_location(fsids, argument.location, csv=True, limit=limit)
91+
fs.historic.get_events_by_location(search_items, argument.location, csv=True, limit=limit)
9192

9293
elif argument.product == 'location.get_detail':
93-
fs.location.get_detail(fsids, argument.location, csv=True, limit=limit)
94+
fs.location.get_detail(search_items, argument.location, csv=True, limit=limit)
9495

9596
elif argument.product == 'location.get_summary':
96-
fs.location.get_summary(fsids, argument.location, csv=True, limit=limit)
97+
fs.location.get_summary(search_items, argument.location, csv=True, limit=limit)
9798

9899
elif argument.product == 'fema.get_nfip':
99-
fs.fema.get_nfip(fsids, argument.location, csv=True, limit=limit)
100+
fs.fema.get_nfip(search_items, argument.location, csv=True, limit=limit)
100101

101102
elif argument.product == 'environmental.get_precipitation':
102-
fs.environmental.get_precipitation(fsids, csv=True, limit=limit)
103+
fs.environmental.get_precipitation(search_items, csv=True, limit=limit)
103104

104105
else:
105106
logging.error("Product not found. Please check that the argument"
106107
" provided is correct: {}".format(argument.product))
107108

108109
else:
109-
raise InvalidArgument("No fsids were provided from either a fsid list or a file. "
110-
"List: '{}', File Name: '{}'".format(argument.fsids, argument.file))
110+
raise InvalidArgument("No search items were provided from either a search item list or a file. "
111+
"List: '{}', File Name: '{}'".format(argument.search_items, argument.file))

firststreet/api/adaptation.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212

1313

1414
class Adaptation(Api):
15-
"""This class receives a list of fsids and handles the creation of a adaptation product from the request.
15+
"""This class receives a list of search_items and handles the creation of a adaptation product from the request.
1616
1717
Methods:
1818
get_detail: Retrieves a list of Adaptation Details for the given list of IDs
1919
get_summary: Retrieves a list of Adaptation Summary for the given list of IDs
2020
"""
2121

2222
def get_detail(self, search_item, csv=False, limit=100, output_dir=None):
23-
"""Retrieves adaptation detail product data from the First Street Foundation API given a list of FSIDs and
24-
returns a list of Adaptation Detail objects.
23+
"""Retrieves adaptation detail product data from the First Street Foundation API given a list of search_items
24+
and returns a list of Adaptation Detail objects.
2525
2626
Args:
2727
search_item (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
@@ -45,7 +45,7 @@ def get_detail(self, search_item, csv=False, limit=100, output_dir=None):
4545

4646
def get_details_by_location(self, search_item, location_type, csv=False, limit=100, output_dir=None):
4747
"""Retrieves adaptation detail product data from the First Street Foundation API given a list of location
48-
FSIDs and returns a list of Adaptation Detail objects.
48+
search_items and returns a list of Adaptation Detail objects.
4949
5050
Args:
5151
search_item (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
@@ -70,11 +70,11 @@ def get_details_by_location(self, search_item, location_type, csv=False, limit=1
7070
api_datas_summary = self.call_api(search_item, "adaptation", "summary", location_type, limit=limit)
7171
summary = [AdaptationSummary(api_data) for api_data in api_datas_summary]
7272

73-
fsids = list(set([adaptation for sum_adap in summary if sum_adap.adaptation for
73+
search_items = list(set([adaptation for sum_adap in summary if sum_adap.adaptation for
7474
adaptation in sum_adap.adaptation]))
7575

76-
if fsids:
77-
api_datas_detail = self.call_api(fsids, "adaptation", "detail", None, limit=limit)
76+
if search_items:
77+
api_datas_detail = self.call_api(search_items, "adaptation", "detail", None, limit=limit)
7878

7979
else:
8080
api_datas_detail = [{"adaptationId": None}]
@@ -89,8 +89,8 @@ def get_details_by_location(self, search_item, location_type, csv=False, limit=1
8989
return [summary, detail]
9090

9191
def get_summary(self, search_item, location_type, csv=False, limit=100, output_dir=None):
92-
"""Retrieves adaptation summary product data from the First Street Foundation API given a list of FSIDs and
93-
returns a list of Adaptation Summary objects.
92+
"""Retrieves adaptation summary product data from the First Street Foundation API given a list of
93+
search_items and returns a list of Adaptation Summary objects.
9494
9595
Args:
9696
search_item (list/file): A First Street Foundation IDs, lat/lng pair, address, or a

firststreet/api/environmental.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212

1313
class Environmental(Api):
14-
"""This class receives a list of fsids and handles the creation of a environmental product from the request.
14+
"""This class receives a list of search_items and handles the creation of a environmental product from the request.
1515
1616
Methods:
1717
get_precipitation: Retrieves a list of Environmental Precipitation for the given list of IDs
1818
"""
1919

2020
def get_precipitation(self, search_item, csv=False, limit=100, output_dir=None):
21-
"""Retrieves environmental precipitation product data from the First Street Foundation API given a list of FSIDs
22-
and returns a list of Environmental Precipitation objects.
21+
"""Retrieves environmental precipitation product data from the First Street Foundation API given a list of
22+
search_items and returns a list of Environmental Precipitation objects.
2323
2424
Args:
2525
search_item (list/file): A First Street Foundation IDs, lat/lng pair, address, or a

firststreet/api/fema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212

1313

1414
class Fema(Api):
15-
"""This class receives a list of fsids and handles the creation of a fema product from the request.
15+
"""This class receives a list of search_items and handles the creation of a fema product from the request.
1616
1717
Methods:
1818
get_nfip: Retrieves a list of Fema Nfip for the given list of IDs
1919
"""
2020

2121
def get_nfip(self, search_item, location_type, csv=False, limit=100, output_dir=None):
22-
"""Retrieves fema nfip product data from the First Street Foundation API given a list of FSIDs and
22+
"""Retrieves fema nfip product data from the First Street Foundation API given a list of search_items and
2323
returns a list of Fema Nfip objects.
2424
2525
Args:

0 commit comments

Comments
 (0)