Prev: L24, Next: L26

# Lecture

📗 The lecture is in person, but you can join Zoom: 8:50-9:40 or 11:00-11:50. Zoom recordings can be viewed on Canvas -> Zoom -> Cloud Recordings. They will be moved to Kaltura over the weekends.
📗 The in-class (participation) quizzes should be submitted on TopHat (Code:741565), but you can submit your answers through Form at the end of the lectures too.
📗 The Python notebooks used during the lectures can also be found on: GitHub. They will be updated weekly.


# Lecture Notes

TopHat Question
➩ Select the location you are sitting in the room.

📗 Map Projections
➩ Positions on a map are usually specified by a longitude and a latitude. It is often used in Geographic Coordinate Systems (GCS).
➩ They are angles in degrees specifying a position on a sphere: Link, Link.
➩ It is difficult to compute areas and distances with angles, so when plotting positions on maps, it is easier to use meters, or Coordinate Reference Systems (CRS).

📗 Regions on Maps are Polygons
➩ A region on a map can be represented by one or many polygons.
➩ A polygon is specified by a list of points connected by line segments.
➩ Information about the polygons are stored in shp, shx and dbf files.

 GeoPandas
GeoPandas package can read shape files into DataFrames and matplotlib can be used to plot them, Link.
geopandas.read_file(...) can be used to read a zip file containing shp, shx and dbf files, and output a GeoDataFrame, which a pandas DataFrame with a column specifying the geometry of the item.
GeoDataFrame.plot() can be used to plot the polygons.

📗 Conversion from GCS to CRS
GeoDataFrame.crs checks the coordinate system used in the data frame.
GeoDataFrame.to_crs("epsg:326??") or GeoDataFrame.to_crs("epsg:326??) can be used to convert from degree-based coordinate system to meter-based coordinate system.
➩ The ?? in the European Petroleum Survey Group (EPSG) code specifies the Universal Transverse Mercator (UTM) zone: Link.
➩ Madison, Wisconsin is in Zone 16.

Madison Map
➩ Find the data on Link on Madison city limit, lakes and rivers, and a list fire stations, and plot them on a map.
➩ Find the largest lake and the fire station closest to the center of the lake.
➩ Code to make the plots: Notebook.
➩ Note: cmap or colormaps specify the colors for the hue visual encoding for a column of numerical values, and the names of the colormaps can be found here: Link.

 Creating Polygons
➩ Polygons can be created manually from the vertices using shapely too: Doc
Point(x, y) creates a point at \(\left(x, y\right)\).
LineString([x1, y1], [x2, y2]) or LineString(Point(x1, y1), Point(x2, y2)) creates a line from \(\left(x_{1}, y_{1}\right)\) to \(\left(x_{2}, y_{2}\right)\).
Polygon([[x1, y1], [x2, y2], ...]) or Polygon([Point(x1, y1), Point(x2, y2), ...]) creates a polygon connecting the vertices \(\left(x_{1}, y_{1}\right)\), \(\left(x_{2}, y_{2}\right)\), ...
box(xmin, ymin, xmax, ymax) is another way to create a rectangular Polygon: Doc.

📗 Polygon Properties
Polygon.area or MultiPolygon.area computes the area of the polygon: Doc.
Polygon.centroid computes the centroid (center) of the polygon: Doc.
Polygon.buffer(r) computes the geometry containing all points within a r distance from the polygon. If Point.buffer(r) is used, the resulting geometry is a circle with radius r around the point, and Point.buffer(r, cap_style = 3) is a square with "radius" r around the point: Doc.

📗 Polygon Manipulation
➩ Union and intersections of polygons are still polygons.
geopandas.overlay(x, y, how = "intersection") computes the polygons that is the intersection of polygons x and y: Doc, if GeoDataFrame has geometry x, GeoDataFrame.intersection(y) computes the same intersection: Doc
geopandas.overlay(x, y, how = "union") computes the polygons that is the union of polygons x and yDoc, if GeoDataFrame has geometry x, GeoDataFrame.union(y) computes the same union: Doc
GeoDataFrame.unary_union is the single combined MultiPolygon of all polygons in the data frame: Doc.
GeoDataFrame.convex_hull computes the convex hull (smallest convex polygon that contains the original polygon): Link, Doc

Madison Map Again
➩ Plot the waters (lakes or rivers) with 2.5 km around the fire stations.
➩ Code to make the plots: Notebook.

 Geocoding
geopy provide geocoding services to convert a text address into a Point geometry that specifies the longitude and latitude of the location: Link
geopandas.tools.geocode(address) returns a Point object with the coordinate for the address.

Geocoding Example
➩ Find and plot a tree with CS building as the root and connects all the fire stations in Madison.
➩ Code to make the plots: Notebook.
➩ One example of such a tree is called the minimum spanning tree and can be constructed greedily by starting with the root and iteratively adding the node closest to any nodes currently in the tree.

Additional Examples
➩ Which ones of the following shapes are convex?
➩ Note: a shape is convex if the shape contains the whole line segment between any two points in the shape: Link.
box(0, 0, 1, 1): yes, all rectangles are convex.
Point(0, 0).buffer(1): yes, all circles (or polygon approximations of circles) are convex.
Polygon([[0, 0], [1, 0], [0, 1]]: yes, any triangle is convex.
box(0, 0, 2, 2).intersect(box(1, 1, 3, 3)): yes, the intersection here is just box(1, 1, 2, 2), and in general, the intersection between any two convex shapes is still convex.
box(0, 0, 2, 2).union(box(1, 1, 3, 3)): no, the line segment between (0, 2) and (1, 3) is not inside the shape.
box(0, 0, 2, 2).union(box(1, 1, 3, 3)).convex_hull: yes, the convex hull of any shape is convex.
Point(0, 0).buffer(1).boundary: no, the boundary is a circle without the inside (approximated by a lot of line segments), and the line segment connecting any two distinct points on the circle is not on the boundary, and in general, the boundary of any shape with strictly positive area is not convex.


 Notes and code adapted from the course taught by Yiyin Shen Link and Tyler Caraza-Harter Link






Last Updated: November 18, 2024 at 11:43 PM