Spatial Analysis Using Python: Key Tools and Techniques

spatial analysis using python md mahin n.w
1 / 20
Embed
Share

Dive into spatial analysis using Python with an overview of spatial objects, tools, examples, and case studies. Learn about Shapely for geometric operations and Geopandas for handling spatial datasets. Discover essential sources and learn how to read different file formats for spatial data analysis.

  • Python
  • Spatial Analysis
  • Geopandas
  • Shapely
  • Data Visualization

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Spatial Analysis using Python Md Mahin 1 UH-DAIS Lab

  2. Spatial Objects 2 Core: Point: (x coordinate, y coordinate) or (longitude, latitude) Example: Any location Line String: Space between two points. Example: Roads Polygon: Collection of points. Example: State Derived: Linear Ring: Lines creating a ring Multi Line String: Multiple line touching each other Geometry Collection: Collection of all types of objects UH-DAIS Lab

  3. Some Examples 3 Google Map Point Street Map States UH-DAIS Lab

  4. Some Important Sources 4 Shape files: Different polygonal data comes in form of shape files, such as: USA Counties: https://catalog.data.gov/dataset/tiger-line-shapefile-current- nation-u-s-counties-and-equivalent-entities Open Street Map: Source for open source street network UH-DAIS Lab

  5. Tools to Handle Spatial Objects 5 Shapely: A polygon library. Easy to perform polygonal operations on it. Geopandas: A python library that makes it smooth to handle spatial datasets Matplotlib: Python tool to visualize data Others: Networkx, OSM to handle road networks, alpha shape to generate polygons from points, numpy to create grids etc. [Each tool offers different services. Often one tool is not enough and we need to combine multiple tools to work] UH-DAIS Lab

  6. Shapely Introduction 6 Objects: Has libraries to support following objects: Points, LineString, Polygons, Multiline, Polygons, Multipolygones, Collection Operations: Supports of finds following operations among shapely objects Find Relations: contains, intersects, overlaps, touches Perform Operations: Intersection, Union UH-DAIS Lab

  7. 7 Case Study UH-DAIS Lab

  8. Reading Files 8 If your data is written in a CSV file: Use normal pandas to read file For csv file, convert objects from string to shapely object Example: import pandas as pd df =pd.read_csv(file_path) df[ geometry ]=shapely.wkt.loads(df[ geometry ][count]) for count in range(len(df[ geometry ])) If your file is written as shapefile Use Geopandas to read file Change the geometric coordinate system (different system provides different types of coordinates) Example: import geopandas as gpd df =pd.read_file(file_path) df = df.to_crs( epsg:4326 ) UH-DAIS Lab

  9. Perform Different Operations Using Shapely and GeoPandas 9 Getting bounds of a polygon: polygon.bounds Getting centroid of a polygon: polygon.centroid Merge all polygons: unary_union(polygones) Find relation among objects using shapely: Polygon1.intersects(polygon2) UH-DAIS Lab

  10. Create Your Own Objects 10 Shapley Point: Shapely.Point(longitude, latitude) Shapley Polygon: Shapely.Polygon([point1, point2 pointn]) Shapely Line String: Shapely.LineString([point1, point2]) UH-DAIS Lab

  11. Create or Handel Grid 11 Use numpy to create or handle grid Use coordinate bounds to get boundary of the grid minx, maxx, miny, maxy = polygon.bounds grid = numpy.mgrid[minx:maxx:10,miny,maxy,10] for x in range(grid[0]): for y in range(grid[1]): point = shapely.Point(grid[0][x],grid[1][y]) UH-DAIS Lab

  12. Contour Plots using Grid 12 Can visualizes two dimensional densities import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1, figsize=(10, 8)) xx,yy = grid \\ use numpy grid data = add_data_to_grid_points(xx,yy) \\ Write a function cfset = ax.contourf(xx, yy, data, levels=[0, 0.8, 1.6, 2.4, 3.2], cmap='coolwarm', alpha=.7) cset = ax.contour(xx, yy, data, levels=[0, 0.8, 1.6, 2.4, 3.2], cmap='coolwarm', alpha=.5) ax.clabel(cset, inline=1, fontsize=10) ax.set_adjustable('box') ax.autoscale(True) h1, l1 = cset.legend_elements() plt.xlabel('Longitude') UH-DAIS Lab plt.ylabel('Latitude') plt.show()

  13. Example 13 UH-DAIS Lab

  14. Spatial Objects Visualization 14 Use mixture of shapely, geopandas and matplotlib Convert shapely object to geopandas dataframe and use matplotlib to visualize import matplotlib.pyplot as plt import geopandas as gpd fig, ax = plt.subplots(1, 1, figsize=(10, 8)) gdf = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[polygon]) gdf.plot(linewidth=0.8, ax=ax, edgecolor='red', color='r', facecolor="none") Plt.show UH-DAIS Lab

  15. Example 15 UH-DAIS Lab

  16. Writing File (csv) 16 Convert data into pandas dataframe and wright import pandas as pd df = pd.DataFrame() df['variable1'] = list1 df['variable2'] = list2 df[ geometry'] = list_geometry df.to_csv(path) UH-DAIS Lab

  17. Writing File (shapefile) 17 Create pandas dataframe Assign coordinate system Convert to geodataframe and write import pandas as pd Import geopandas as gpd df = pd.DataFrame() df['variable1'] = list1 df[ geometry'] = list_geometry crs = {'init': 'epsg:4326'} gdf = gpd.GeoDataFrame(df, crs= crs, geometry = df.geometry) gdf.to_file(path) UH-DAIS Lab

  18. Visualizing Earthquack(code) 18 import pandas as pd from shapely.geometry import Point import matplotlib.pyplot as plt import geopandas as gpd fig, ax = plt.subplots(1, 1, figsize=(10, 8)) df = pd.read_csv(eartquack.csv) contiguousUSA_shape = gpd.read_file(contiguousUSA.shp) contiguousUSA_shape = contiguousUSA_shape.to_crs("epsg:4326") gdf = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[contiguousUSA_shape['geometry'][0]]) gdf.plot(linewidth=0.8, ax=ax, edgecolor='red', color='r', facecolor="none") for count in range(len(df['latitudes'])): point = Point([df['longitudes'][count],df['latitudes'][count]]) gdf = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[point]) gdf.plot(linewidth=0.8, ax=ax, edgecolor='green', color='green', facecolor="none") plt.show() UH-DAIS Lab

  19. Output 19 UH-DAIS Lab

  20. References 20 1. https://shapely.readthedocs.io/en/stable/manual.html 2. https://geopandas.org/en/stable/docs.html 3. https://matplotlib.org/stable/index.html 4. https://spatialanalysis.github.io/handsonspatialdata/basic-mapping.html UH-DAIS Lab

More Related Content