patternpythonMinor
The Geometry of Maps - Twitter Trend
Viewed 0 times
themapstrendgeometrytwitter
Problem
Below assignment is taken from here.
Introduction
In this project, you will develop a geographic visualization of
twitter data across the USA. You will need to use dictionaries, lists,
and data abstraction techniques to create a modular program. Below
assignment is
this project is available
here.
Phase 2: The Geometry of Maps
Positions
We will use the position abstract data type to represent
geographic latitude-longitude positions on the Earth. The data
abstraction, defined at the top of
In this phase, you will write two functions that together determine
the centers of U.S. states. The shape of a state is represented as a
list of polygons. Some states (e.g. Hawaii) consist of multiple
polygons, but most states (e.g. Colorado) consist of only one polygon
(still represented as a length-one list).
Problem 6 (2 pt). Implement
its area. The input polygon is represented as a list of position
abstract data types, which are the consecutive vertices of its
perimeter. The first vertex is always identical to the last.
The centroid of a two-dimensional shape is its center of balance,
defined as the intersection of all straight lines that evenly divide
the shape into equal-area halves.
and area of an individual polygon.
The formula for computing the centroid of a polygon appears on
Wikipedia. The formula relies on vertices being consecutive (either
clockwise or counterclockwise; both give the same answer), a property
that you may assume always holds for the input.
When you complete this problem, the doctest for
pass.
Problem 7
Introduction
In this project, you will develop a geographic visualization of
twitter data across the USA. You will need to use dictionaries, lists,
and data abstraction techniques to create a modular program. Below
assignment is
phase 2 of this project. For reference, phase 1 ofthis project is available
here.
Phase 2: The Geometry of Maps
Positions
We will use the position abstract data type to represent
geographic latitude-longitude positions on the Earth. The data
abstraction, defined at the top of
geo.py, has the constructormake_position and the selectors latitude and longitude.In this phase, you will write two functions that together determine
the centers of U.S. states. The shape of a state is represented as a
list of polygons. Some states (e.g. Hawaii) consist of multiple
polygons, but most states (e.g. Colorado) consist of only one polygon
(still represented as a length-one list).
Problem 6 (2 pt). Implement
find_centroid, which takes a polygon and returns three values: the coordinates of its centroid andits area. The input polygon is represented as a list of position
abstract data types, which are the consecutive vertices of its
perimeter. The first vertex is always identical to the last.
The centroid of a two-dimensional shape is its center of balance,
defined as the intersection of all straight lines that evenly divide
the shape into equal-area halves.
find_centroid returns the centroidand area of an individual polygon.
The formula for computing the centroid of a polygon appears on
Wikipedia. The formula relies on vertices being consecutive (either
clockwise or counterclockwise; both give the same answer), a property
that you may assume always holds for the input.
When you complete this problem, the doctest for
find_centroid shouldpass.
python3 trends.py -t find_centroidProblem 7
Solution
A few notes about your code:
-
Whenever possible, try to use object-based loops instead of index-based loops. It makes the code cleaner and lowers the cognitive overhead. For example, turn this loop:
Into this one:
You could even simplify this with the built-in function
Or with a list comprehension:
-
The following piece of code:
...would benefit from the built-in function
-
You are importing waaaayyyyy too many modules and features that you don't use. Please try to only include what you will use, it will make it simpler for you (and other people reading your code) to know what your code really relies on.
-
Whenever possible, try to use object-based loops instead of index-based loops. It makes the code cleaner and lowers the cognitive overhead. For example, turn this loop:
centroid_and_area_of_all_polygons = []
for index in range(len(polygons)):
centroid_and_area_of_all_polygons.append(find_centroid(polygons[index]))Into this one:
centroid_and_area_of_all_polygons = []
for polygon in polygons:
centroid_and_area_of_all_polygons.append(find_centroid(polygon))You could even simplify this with the built-in function
map:centroid_and_area_of_all_polygons = list(map(find_centroid, polygons))Or with a list comprehension:
centroid_and_area_of_all_polygons = [find_centroid(polygon) for polygon in polygons]-
The following piece of code:
if area_of_polygon < 0:
return (centroid_latitude, centroid_longitude, -area_of_polygon)
else:
return (centroid_latitude, centroid_longitude, area_of_polygon)...would benefit from the built-in function
abs:return (centroid_latitude, centroid_longitude, abs(area_of_polygon))-
You are importing waaaayyyyy too many modules and features that you don't use. Please try to only include what you will use, it will make it simpler for you (and other people reading your code) to know what your code really relies on.
Code Snippets
centroid_and_area_of_all_polygons = []
for index in range(len(polygons)):
centroid_and_area_of_all_polygons.append(find_centroid(polygons[index]))centroid_and_area_of_all_polygons = []
for polygon in polygons:
centroid_and_area_of_all_polygons.append(find_centroid(polygon))centroid_and_area_of_all_polygons = list(map(find_centroid, polygons))centroid_and_area_of_all_polygons = [find_centroid(polygon) for polygon in polygons]if area_of_polygon < 0:
return (centroid_latitude, centroid_longitude, -area_of_polygon)
else:
return (centroid_latitude, centroid_longitude, area_of_polygon)Context
StackExchange Code Review Q#91279, answer score: 4
Revisions (0)
No revisions yet.