forked from RaptorMaps/raptormaps-api-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_geojson_objects.py
More file actions
67 lines (44 loc) · 2.21 KB
/
get_geojson_objects.py
File metadata and controls
67 lines (44 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import os
"""
This is a simple example of a script one might use to get geojson objects from a specific farm.
In the script, we find a farm ID based on the farm name, pull an informational summary about the farm, and use that summary data to iterate through the geojson request.
You will need to provide a bearer token in order to run this script. You can find your bearer token by following the directions in the API docs found at https://docs.raptormaps.com/reference/get-api-access-token.
Additionally, you will need to know your organizational ID and the name of the farm you are interested in.
"""
bearer_token = os.environ['BEARER_TOKEN']
auth_header = {"authorization": "Bearer {}".format(bearer_token)}
base_url = 'https://api.raptormaps.com'
org_id = <your org id>
# get farm_id from /sorted_solar_farms response body by querying by name
def get_farm_id(farm_name):
res = requests.get(
f'{base_url}/sorted_solar_farms?org_id={org_id}&name={farm_name}', headers=auth_header)
farm_id = res.json()['farms'][0]['id']
return farm_id
# Get the summary object for the farm in question
def get_farm_summary(farm_id):
res = requests.get(
f'{base_url}/solar_farms/{farm_id}/summary?org_id={org_id}', headers=auth_header)
rows = res.json()['rows']
return rows
# For this example, we iteratively retrieve the row objects. Other object types can be retrieved in the same way by changing the object_type parameter in the request.
def get_row_objects(rows, farm_id):
i = 0
row_data = []
while i <= rows:
res = requests.get(f'/solar_farms/{base_url}/objects/geojson?org_id={org_id}&object_type=row&offset={i}&limit=20',
headers=auth_header)
row_data.append(res.json())
i = i+10
return row_data
# This function calls the other functions in this script and prints the row data to the console. It might be more useful to dump the data to a file.
def get_row_info_by_farm_name(name):
farm_id = get_farm_id(name)
row_num = get_farm_summary(farm_id)
row_data = get_row_objects(row_num, farm_id)
print(row_data)
def main():
get_row_info_by_farm_name('<your farm name here>')
if __name__ == '__main__':
main()