-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrape.py
185 lines (143 loc) · 4.8 KB
/
scrape.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import glob
import json
import csv
import re
from bs4 import BeautifulSoup
from fixes import (
countries_with_states,
unicode_replacements,
riocuarto_data,
target_rock_labels,
replace_age,
boolean_map
)
def parse_dms_to_decimal(dms):
''' given a dms value in this data, return decimal equivalent '''
parts = [x for x in re.split('[°\'" ]+', dms) if x]
direction = parts[0]
degrees = parts[1]
minutes = parts[2]
seconds = 0.0
if len(parts) == 4:
seconds = parts[3]
dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60)
if direction in ('S', 'W'):
dd *= -1
return round(dd, 4)
pages = glob.glob('pages/*.html')
filename_out = 'earth-impact-craters'
geojson = {
'type': 'FeatureCollection',
'features': []
}
csv_data = []
csv_headers = [
'crater_name',
'state',
'country',
'target_rock',
'diameter_km',
'age_millions_years_ago',
'exposed',
'drilled',
'bolid_type',
'latitude',
'longitude',
'url',
'notes'
]
# loop over the saved pages
for page in pages:
# open the page and read in the HTML
with open(page, 'r') as infile:
html = infile.read()
# the page for Rio Cuarto 404s, but data is at
# http://www.passc.net/EarthImpactDatabase/New%20website_05-2018/SouthAmerica.html # noqa
if 'Riocuarto' in page:
csv_data.append(riocuarto_data['csv'])
geojson['features'].append(riocuarto_data['feature'])
continue
soup = BeautifulSoup(html, 'html.parser')
main = soup.find('main')
# grab the crater name
crater_name = main.find('h1').text.strip()
# do a little unicode cleanup
for pair in unicode_replacements:
crater_name = crater_name.replace(*pair)
# locate the table with the data in it
tables = main.find_all('table')
data_table, references = tables
# this table has but two rows, my friend
headers, data = data_table.find_all('tr')
# love it when a plan comes together
_, location, latitude, longitude, diameter, age, exposed, drilled, target_rock, bolid_type = [x.text.strip() for x in data.find_all('td')] # noqa
# look up the target rock value
target_rock = target_rock_labels.get(target_rock, '')
# parse the lat/lng values
lat = parse_dms_to_decimal(latitude.replace('Â', ''))
lng = parse_dms_to_decimal(longitude.replace('Â', ''))
# suss out state/country data
state = ''
country = location
if countries_with_states.get(country):
state, country = countries_with_states.get(country)
# fix some wonk in the diameter values
for char in ['(', ')', '*', '~', 'km']:
diameter = diameter.replace(char, '')
# ... and the age values
for pair in replace_age:
age = age.replace(*pair)
# get booleans for the `exposed` and `drilled` values
exposed = boolean_map.get(exposed, '')
drilled = boolean_map.get(drilled, '')
# we want blanks not dashes for empty bolid types
if bolid_type == '-':
bolid_type = ''
# build the URL
url = f'http://www.passc.net/EarthImpactDatabase/New%20website_05-2018/{page.split("/")[-1]}' # noqa
# build the props dict
props = {
'crater_name': crater_name,
'state': state,
'country': country,
'target_rock': target_rock,
'diameter_km': diameter,
'age_millions_years_ago': age,
'exposed': exposed,
'drilled': drilled,
'bolid_type': bolid_type,
'url': url
}
# add a note to the Kärdla record
if 'Kardla' in url:
props['notes'] = 'The diameter of the Kärdla impact crater is under review.' # noqa
# add a note to the Santa Fe record
if '6-13' in diameter and 'SantaFe' in url:
props['diameter_km'] = 6.0
props['notes'] = 'The diameter of the Santa Fe crater is estimated at 6-13 kilometers.' # noqa
# coerce diameter values to floats
props['diameter_km'] = float(props['diameter_km'])
# build the geojson feature record
d = {
'type': 'Feature',
'properties': props,
'geometry': {
'type': 'Point',
'coordinates': [lng, lat]
}
}
# add the record to the geojson feature collection
geojson['features'].append(d)
# drop a copy of this data (w/ coords) in the csv list
data_for_csv = props.copy()
data_for_csv['latitude'] = lat
data_for_csv['longitude'] = lng
csv_data.append(data_for_csv)
# write data to file
with open(f'{filename_out}.geojson', 'w') as outfile_gj, open(f'{filename_out}.csv', 'w') as outfile_csv: # noqa
# write geojson data
outfile_gj.write(json.dumps(geojson))
# write csv data
writer = csv.DictWriter(outfile_csv, fieldnames=csv_headers)
writer.writeheader()
writer.writerows(csv_data)