PYTHON HELP MAKING CHANGES TO THE MAP FILE AND PRODUCING A GEOJSON FILE. I NEED
ID: 3828713 • Letter: P
Question
PYTHON HELP MAKING CHANGES TO THE MAP FILE AND PRODUCING A GEOJSON FILE. I NEED A SNAPSHOT OF THE MAP. WHEN DONE.
make the necessary changes to map.py to produce a file_houston.geojson file. Go out to http://geojsonlint.com/ to test your file. Remember to open your .geojson file in notepad, select FeatureCollection on the web site, then copy and paste the code into the box provided to see your data plotted on a map. You may not be able to import your parse.py file because of timeout issues. If this is the case, just copy the necessary code into your graph.py and map.py programs.
HERES THE CODE:
"""
Data Visualization Project
Parse data from an ugly CSV or Excel file, and render it in
JSON-like form, visualize in graphs, and plot as a map.
Part III: Take the data we parsed earlier and create a different format
for rendering a map. Here, we parse through each line item of the
CSV file and create a geojson object, to be collected into one geojson
file for uploading to gist.github.com.
"""
import geojson
import parse as p
def create_map(data_file):
"""Creates a GeoJSON file.
Returns a GeoJSON file that can be rendered in a GitHub
Gist at gist.github.com. Just copy the output file and
paste into a new Gist, then create either a public or
private gist. GitHub will automatically render the GeoJSON
file as a map.
"""
# Define type of GeoJSON we're creating
geo_map = {"type": "FeatureCollection"}
# Define empty list to collect each point to graph
item_list = []
# Iterate over our data to create GeoJSOn document.
# We're using enumerate() so we get the line, as well
# the index, which is the line number.
for index, line in enumerate(data_file):
# Skip any zero coordinates as this will throw off
# our map.
if line['X'] == "0" or line['Y'] == "0":
continue
# Setup a new dictionary for each iteration.
data = {}
# Assign line items to appropriate GeoJSON fields.
data['type'] = 'Feature'
data['properties'] = {'title': line['Category'],
'description': line['Descript'],
'date': line['Date']}
data['geometry'] = {'type': 'Point',
'coordinates': (float(line['X']), float(line['Y']))}
# Add data dictionary to our item_list
item_list.append(data)
# For each point in our item_list, we add the point to our
# dictionary. setdefault creates a key called 'features' that
# has a value type of an empty list. With each iteration, we
# are appending our point to that list.
for point in item_list:
geo_map.setdefault('features', []).append(point)
# Now that all data is parsed in GeoJSON write to a file so we
# can upload it to gist.github.com
with open('file_sf.geojson', 'w') as f:
f.write(geojson.dumps(geo_map))
def main():
data = p.parse(p.MY_FILE, ",")
return create_map(data)
if __name__ == '__main__':
main()
Explanation / Answer
import geojson
import parse as p
def create_map(data_file):
"""Creates a GeoJSON file.
Returns a GeoJSON file that can be rendered in a GitHub
Gist at gist.github.com. Just copy the output file and
paste into a new Gist, then create either a public or
private gist. GitHub will automatically render the GeoJSON
file as a map.
"""
# Define type of GeoJSON we're creating
geo_map = {"type": "FeatureCollection"}
# Define empty list to collect each point to graph
item_list = []
# Iterate over our data to create GeoJSOn document.
# We're using enumerate() so we get the line, as well
# the index, which is the line number.
for index, line in enumerate(data_file):
# Skip any zero coordinates as this will throw off
# our map.
if line['X'] == "0" or line['Y'] == "0":
continue
# Setup a new dictionary for each iteration.
data = {}
# Assign line items to appropriate GeoJSON fields.
data['type'] = 'Feature'
data['properties'] = {'title': line['Category'],
'description': line['Descript'],
'date': line['Date']}
data['geometry'] = {'type': 'Point',
'coordinates': (float(line['X']), float(line['Y']))}
# Add data dictionary to our item_list
item_list.append(data)
# For each point in our item_list, we add the point to our
# dictionary. setdefault creates a key called 'features' that
# has a value type of an empty list. With each iteration, we
# are appending our point to that list.
for point in item_list:
geo_map.setdefault('features', []).append(point)
# Now that all data is parsed in GeoJSON write to a file so we
# can upload it to gist.github.com
with open('file_sf.geojson', 'w') as f:
f.write(geojson.dumps(geo_map))
def main():
data = p.parse(p.MY_FILE, ",")
return create_map(data)
if __name__ == '__main__':
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.