Skip to content

Commit

Permalink
feat: Add examples/geospatial-python-urban-analysis-with-postgis project
Browse files Browse the repository at this point in the history
  • Loading branch information
BrenQ committed Feb 14, 2025
1 parent a94258d commit 97b25fa
Show file tree
Hide file tree
Showing 72 changed files with 14,545 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/geospatial-python-urban-analysis-with-postgis/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Base image with Python
FROM python:3.12

# Set the working directory
WORKDIR /app

# Install dependencies
RUN apt-get update && apt-get install -y \
libgdal-dev \
&& rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the script
COPY transport.py .

# Run the script when the container starts
CMD ["python", "transport.py"]
139 changes: 139 additions & 0 deletions examples/geospatial-python-urban-analysis-with-postgis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# 🌍 Geospatial Urban Analysis Project

## 📌 Overview

This project focuses on **geospatial data analysis** for urban environments, particularly analyzing **pedestrian zones, transportation networks, census data, and geographic boundaries**. The dataset includes **shapefiles, GeoJSON, Parquet, and raster files**, allowing advanced **spatial processing and visualization**.

The project uses **PostgreSQL with PostGIS**, **Docker**, and **GeoPandas**, enabling **efficient spatial queries, ETL pipelines, and geospatial machine learning models**.

### **Key Features**

- 🏙 **Urban Infrastructure Analysis**: Analyzes bike paths, subway entrances, and school locations.
- 📊 **Geospatial Data Processing**: Supports various spatial formats (Shapefile, GeoJSON, Parquet, Raster).
- 🔄 **ETL Pipelines**: Extract, transform, and load urban data into **PostGIS**.
- 🤖 **Geospatial Machine Learning**: Clustering models to optimize urban planning decisions.
- 🗺 **Interactive Mapping**: Generates visualizations using **Folium and Matplotlib**.

---

## 🛠 **Requirements**

Before running the project, ensure you have the following dependencies installed:

### 💻 **System Requirements**

- 🐳 **Docker** (for PostgreSQL with PostGIS)
- 🐍 **Python 3.8+**

### 📦 **Python Dependencies**

All required Python libraries are listed in `requirements.txt`. Install them using:

```sh
pip install -r requirements.txt
```

Main dependencies:

- 🌍 **GeoPandas**: Geospatial data processing.
- 🗄 **PostgreSQL & PostGIS**: Geospatial database support.
- 📈 **Matplotlib & Folium**: Data visualization.
- 🤖 **Scikit-learn**: Clustering and machine learning models.

---

## 🚀 **Setup & Installation**

### 📂 **1. Clone the Repository**

```sh
git clone [email protected]:nanlabs/backend-reference.git
cd examples/geospatial-python-urban-analysis-with-postgis
```

### 🏗 **2. Set Up a Virtual Environment**

Create and activate a Python virtual environment:

```sh
python -m venv env
source env/bin/activate # On macOS/Linux
env\Scripts\activate # On Windows
```

Once activated, install dependencies:

```sh
pip install -r requirements.txt
```

### 🐳 **3. Set Up Docker with PostgreSQL and PostGIS**

Ensure that **Docker** is installed and running. Then, start the database with:

```sh
docker-compose up -d
```

This will:

- 🛢 Start a **PostgreSQL database** with **PostGIS** extensions enabled.
- 📌 Create the necessary **database schema** for storing geospatial data.

## 📖 **Working with Notebooks**

To start the analysis and visualization:

```sh
jupyter notebook
```

Then, open one of the notebooks in the `notebooks/` directory.

The notebooks cover:

- 🌍 **Geospatial Data Exploration**: Loading and visualizing spatial datasets.
- 🚇 **Urban Accessibility Analysis**: Assessing accessibility of public transport.
- 🤖 **Clustering and Machine Learning**: Applying spatial clustering algorithms.


Check failure on line 99 in examples/geospatial-python-urban-analysis-with-postgis/README.md

View workflow job for this annotation

GitHub Actions / Markdownlint / Markdown Lint

Multiple consecutive blank lines [Expected: 1; Actual: 2]
### 📌 **Pipelines Overview**

Check failure on line 100 in examples/geospatial-python-urban-analysis-with-postgis/README.md

View workflow job for this annotation

GitHub Actions / Markdownlint / Markdown Lint

Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "### 📌 **Pipelines Overview**"]
The project includes **several geospatial data processing pipelines**, located in `src/pipelines/`:

- 🚌 **`bus_stop_analysis.py`**: Analyzes bus stops and their spatial distribution.
- 📍 **`optimal_stop_pipeline.py`**: Computes the best locations for public transportation stops.
- 🗺 **`shapefile_to_raster.py`**: Converts vector-based shapefiles into raster format for GIS applications.

### ⚙️ **Running Pipelines**

Check failure on line 107 in examples/geospatial-python-urban-analysis-with-postgis/README.md

View workflow job for this annotation

GitHub Actions / Markdownlint / Markdown Lint

Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "### ⚙️ **Running Pipelines**"]
To execute a pipeline, use the following command:

```sh
PYTHON=. python -m src.pipelines.bus_stop_analysis
```

Replace `bus_stop_analysis` with the pipeline you want to run.

Each pipeline processes geospatial data **efficiently**, ensuring the data is ready for **urban planning and visualization**.

---

## 🏗 **Project Structure**

```sh
.
├── Dockerfile # 🐳 Docker configuration for Python environment
├── docker-compose.yml # 🛢 PostgreSQL + PostGIS setup
├── requirements.txt # 📦 Python dependencies
├── config.py # ⚙️ Configuration settings
├── data/ # 🌍 Raw geospatial datasets
├── notebooks/ # 📖 Jupyter Notebooks for geospatial analysis
├── scripts/ # 🔄 Data processing scripts
├── src/ # 🏗 Source code
│ ├── database/ # 🗄 Database connection and queries
│ ├── etl/ # 🔄 ETL pipeline for spatial data
│ ├── ml/ # 🤖 Machine learning models for clustering
│ ├── pipelines/ # 📌 Spatial data processing workflows
│ ├── visualization/ # 🗺 Map and data visualization modules
```

---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DB_CONNECTION = "postgresql://user:password@host:port/dbname"
POPULATION_DENSITY_RASTER = "data/population_density.tif"
DISTRICTS_URL = "https://cdn.buenosaires.gob.ar/datosabiertos/datasets/ministerio-de-educacion/comunas/comunas.geojson"
EPSG_TARGET = 3857 # Standard coordinate reference system (CRS)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UTF-8
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["WGS84(DD)", DATUM["WGS84", SPHEROID["WGS84", 6378137.0, 298.257223563]], PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4326"]]
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UTF-8
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["WGS84(DD)", DATUM["WGS84", SPHEROID["WGS84", 6378137.0, 298.257223563]], PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4326"]]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["POSGAR_94_Argentina_3",GEOGCS["GCS_POSGAR 94",DATUM["D_POSGAR_1994",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",-90],PARAMETER["central_meridian",-66],PARAMETER["scale_factor",1],PARAMETER["false_easting",3500000],PARAMETER["false_northing",0],UNIT["Meter",1]]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["POSGAR 94 / Argentina 3",GEOGCS["POSGAR 94",DATUM["Posiciones_Geodesicas_Argentinas_1994",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6694"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4694"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",-90],PARAMETER["central_meridian",-66],PARAMETER["scale_factor",1],PARAMETER["false_easting",3500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","22183"]]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 97b25fa

Please sign in to comment.