Skip to content

Python commands to upload and manage geospatial data in GeoDataFrames. A concise, step-by-step guide for seamless data handling.

Notifications You must be signed in to change notification settings

oechenique/boundaries

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 

Repository files navigation

Managing Geospatial Data with Python: A Guide to Loading and Manipulating GeoDataFrames

Explore different techniques for handling geospatial data using Python and GeoPandas. Additionally, check out some related libraries.

TABLE OF CONTENTS

  1. Shapefile
  2. GeoJSON
  3. KML
  4. WFS (Web Feature Service)
  5. GIS Web Service
  6. H3 (UBER H3 Hexagon)
  7. 3D Visualization
  8. Other File Formats

Loading Shapefiles (.shp)

Refer to the shapefile notebook for detailed examples and Python code.

import geopandas as gpd

# Path to the shapefile
shp_path = 'path/to/file.shp'

# Load the shapefile into a GeoDataFrame
gdf_shapefile = gpd.read_file(shp_path)

# Display the GeoDataFrame
print(gdf_shapefile.head())

Loading GeoJSON (.geojson)

Check out the geojson notebook for detailed examples and Python code.

import geopandas as gpd

# Path to the GeoJSON file
geojson_path = 'path/to/file.geojson'

# Load the GeoJSON into a GeoDataFrame
gdf_geojson = gpd.read_file(geojson_path)

# Display the GeoDataFrame
print(gdf_geojson.head())

Loading KML (.kml)

Explore the kml notebook for a detailed KML loading example.

Note: If you can display the notebook, same code here: kml md.

import geopandas as gpd

# Path to the KML file
kml_path = 'path/to/file.kml'

# Load the KML into a GeoDataFrame
gdf_kml = gpd.read_file(kml_path)

# Display the GeoDataFrame
print(gdf_kml.head())

Accessing Web Feature Services (WFS)

Refer to the wfs notebook for detailed examples and Python code.

from owslib.wfs import WebFeatureService
import json
import geopandas as gpd

# WFS (Web Feature Service) URL
wfs_url = 'https://your-web-service-url/geoserver/ows?'
version = '1.1.0'

# Create a WFS object
wfs = WebFeatureService(url=wfs_url, version=version)

# Get feature data from WFS using specified parameters
typename = 'your-layer-name'
srsname = 'EPSG:4326'
output_format = 'application/json'
response = wfs.getfeature(typename=typename, srsname=srsname, outputFormat=output_format)

# Read the response data
data = response.read()

# Load data into a dictionary
data_dict = json.loads(data)

# Save data to a temporary GeoJSON file
with open('temp.geojson', 'w') as geojson_file:
    json.dump(data_dict, geojson_file)

# Read data from the GeoJSON file into a GeoDataFrame
gdf = gpd.read_file('temp.geojson')

# Set a Coordinate Reference System (CRS) for the GeoDataFrame
gdf = gdf.set_crs(epsg=4326, allow_override=True)

Using GIS Web Services

Explore the gis web services notebook for examples on accessing GIS Web Services such as OpenStreetMap and Google Maps, and learn how to retrieve relevant geospatial information using Python.

import osmnx as ox
import geopandas as gpd

# Specify the name of the city or area of interest
city = "Name of the City or Area of Interest"

# Download OSM data using Overpass API
tags = {"amenity": "bar"}
gdf = ox.geometries_from_place(city, tags)

# Filter and create a GeoPandas DataFrame with relevant data
gdf = gdf[gdf['amenity'] == 'bar']

gdf = gdf.reset_index()

# Filter only Point geometries
gdf = gdf[gdf['geometry'].geom_type == 'Point']

# Display the resulting DataFrame
print(gdf)

Creating H3 Hexagons

Refer to the h3 notebook for detailed examples and Python code.

import h3
import geopandas as gpd
from shapely.geometry import Polygon

# Specify the resolution for H3 hexagons
h3_resolution = 9

# Create a GeoDataFrame to hold the H3 data
gdf_h3 = gpd.GeoDataFrame()

# Define the bounding box of the area of interest
bbox = Polygon([(min_longitude, min_latitude), (min_longitude, max_latitude),
                (max_longitude, max_latitude), (max_longitude, min_latitude)])

# Get the H3 hexagons covering the bounding box
hexagons = list(h3.polyfill(bbox.exterior.coords, res=h3_resolution))

# Create a GeoDataFrame with the hexagons
gdf_h3['geometry'] = gpd.GeoSeries([Polygon(h3.h3_to_geo_boundary(hexagon)) for hexagon in hexagons])
gdf_h3['hex_id'] = hexagons

# Display the resulting GeoDataFrame
print(gdf_h3)

3D Visualization of Geospatial Data

Explore the 3D visualization notebook to learn how to visualize geospatial data with a focus on the Z-axis. This notebook demonstrates techniques for creating stunning 3D visualizations using Python.

# DataFrame
df = pd.read_csv('hex-data.csv')
map_1.add_data(data=df, name='data_1')

# CSV
with open('csv-data.csv', 'r') as f:
    csvData = f.read()
map_1.add_data(data=csvData, name='data_2')

# GeoJSON as string
with open('sf_zip_geo.json', 'r') as f:
    geojson = f.read()

map_1.add_data(data=geojson, name='geojson')

Handling Other File Formats

Explore the others md for detailed examples and Python code.

About

Python commands to upload and manage geospatial data in GeoDataFrames. A concise, step-by-step guide for seamless data handling.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published