-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (87 loc) · 2.96 KB
/
main.py
File metadata and controls
106 lines (87 loc) · 2.96 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import requests
from dataclasses import dataclass
from typing import List, Optional, Union
import json
# The API endpoint for USACE NSI structures
url = "https://nsi.sec.usace.army.mil/nsiapi/structures?bbox=-81.58418,30.25165,-81.58161,30.26939,-81.55898,30.26939,-81.55281,30.24998,-81.58418,30.25165"
@dataclass
class Geometry:
type: str
coordinates: List[float]
@dataclass
class FeatureProperties:
fd_id: int
bid: str
occtype: str
st_damcat: str
bldgtype: str
found_type: str
cbfips: int
pop2amu65: int
pop2amo65: int
pop2pmu65: int
pop2pmo65: int
sqft: float
num_story: int
ftprntid: str
ftprntsrc: Optional[str]
students: int
found_ht: float
val_struct: float
val_cont: float
val_vehic: float
source: str
med_yr_blt: int
firmzone: Optional[str]
o65disable: float
u65disable: float
x: float
y: float
ground_elv: float
ground_elv_m: float
@dataclass
class Feature:
type: str
geometry: Geometry
properties: FeatureProperties
@dataclass
class FeatureCollection:
type: str
features: List[Feature]
def parse_geojson(data_dict):
feature_list = []
for feat in data_dict['features']:
# Create Geometry object
geom = Geometry(**feat['geometry'])
# Create Properties object
prop = FeatureProperties(**feat['properties'])
# Create Feature object
feature_list.append(Feature(type=feat['type'], geometry=geom, properties=prop))
return FeatureCollection(type=data_dict['type'], features=feature_list)
def download_nsi_data(target_url, output_file):
try:
# Send a GET request to the URL
response = requests.get(target_url)
# Raise an exception for bad status codes (4xx or 5xx)
response.raise_for_status()
# Parse the response content as JSON
data = response.json()
print(f"Successfully downloaded data")
# Save the JSON data to a file with pretty formatting
collection = parse_geojson(data)
# Calculate Stats
total_val = sum(f.properties.val_struct for f in collection.features)
total_sqft = sum(f.properties.sqft for f in collection.features)
avg_sqft = total_sqft / len(collection.features)
occ_types = [f.properties.occtype for f in collection.features]
print(f"--- Analysis Summary ---")
print(f"Features Processed: {len(collection.features)}")
print(f"Total Structural Value: ${total_val:,.2f}")
print(f"Average Square Footage: {avg_sqft:,.2f} sqft")
print(f"Unique Occupancy Types: {set(occ_types)}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
except json.JSONDecodeError:
print("Failed to decode JSON. The response might not be in JSON format.")
if __name__ == "__main__":
download_nsi_data(url, "nsi_structures.json")