Making maps from geographic data is a powerful way to analyze patterns, understand locations, and visualize relationships between data and place.
This guide will explain everything step-by-step in simple terms, and we’ll use Python (a beginner-friendly programming language) with easy libraries.
1️⃣ What Is Geographic Data?
Geographic data (also called spatial data) is any data that contains location information.
There are two main types:
A) Vector Data (Most Common)
Represents features as:
Common file types:
.shp (Shapefile).geojson.gpkgB) Raster Data
Represents data as pixels (like images):
File types:
.tif.jpg.png2️⃣ Important Geographic Concepts (Simple Explanation)
🌍 Latitude and Longitude
The Earth uses coordinates:
Example:
CityLatitudeLongitudeNew York40.7128-74.0060London51.5074-0.1278
Markdown version (copy-paste ready):
| City | Latitude | Longitude | |-----------|----------|-----------| | New York | 40.7128 | -74.0060 | | London | 51.5074 | -0.1278 |
🗺️ Coordinate Reference System (CRS)
CRS tells the computer how the Earth is projected onto a flat map.
The most common CRS:
EPSG:4326It uses latitude and longitude.
3️⃣ Tools We’ll Use (Python)
We’ll use:
pandas → handles data tablesgeopandas → handles geographic datamatplotlib → plots mapsfolium → interactive web mapsInstall them:
pip install pandas geopandas matplotlib folium
4️⃣ Example 1: Making a Simple Map from Latitude & Longitude
Step 1: Create Sample Data
import pandas as pd
data = {
"City": ["New York", "London", "Tokyo"],
"Latitude": [40.7128, 51.5074, 35.6895],
"Longitude": [-74.0060, -0.1278, 139.6917]
}
df = pd.DataFrame(data)
print(df)
Step 2: Convert to Geographic Data
import geopandas as gpd from shapely.geometry import Point geometry = [Point(xy) for xy in zip(df["Longitude"], df["Latitude"])] gdf = gpd.GeoDataFrame(df, geometry=geometry) gdf.set_crs(epsg=4326, inplace=True) print(gdf)
Step 3: Plot the Map
import matplotlib.pyplot as plt
gdf.plot(color="red", markersize=100)
plt.title("City Locations")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()
You now created your first geographic map 🎉
5️⃣ Example 2: Making an Interactive Map (Beginner Friendly)
Interactive maps are better for beginners because you can zoom and click.
import folium
# Create a base map
m = folium.Map(location=[20, 0], zoom_start=2)
# Add markers
for index, row in df.iterrows():
folium.Marker(
location=[row["Latitude"], row["Longitude"]],
popup=row["City"]
).add_to(m)
m
This creates a zoomable interactive map.
6️⃣ Working with Real Geographic Files (Shapefiles)
Example: Load a country boundary file.
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
print(world.head())
Plot it:
world.plot(figsize=(10, 6))
plt.title("World Map")
plt.show()
7️⃣ Choropleth Maps (Color Based on Data)
A choropleth map colors regions based on values.
Example: Color countries by population.
world.plot(column="pop_est", cmap="OrRd", legend=True)
plt.title("Population by Country")
plt.show()
What happens:
column="pop_est" → use population datacmap="OrRd" → color stylelegend=True → show color scale8️⃣ Spatial Analysis (Basic Ideas)
Once data is on a map, we can:
Example: Calculate distance between two cities
from geopy.distance import geodesic
ny = (40.7128, -74.0060)
london = (51.5074, -0.1278)
distance = geodesic(ny, london).km
print("Distance:", distance, "km")
9️⃣ Common Workflow in Geographic Data Analysis
Here’s how professionals usually work:
| Step | Description | |------|------------| | 1 | Collect geographic data | | 2 | Clean and format data | | 3 | Convert to GeoDataFrame | | 4 | Check CRS | | 5 | Visualize data | | 6 | Perform spatial analysis | | 7 | Export results |
🔟 Exporting Your Map
Save to file:
gdf.to_file("cities.shp")
Save interactive map:
m.save("map.html")
FULL COMPILED CODE (All Code Together)
Below is all the code combined into one script:
# Install packages first:
# pip install pandas geopandas matplotlib folium geopy
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import folium
from shapely.geometry import Point
from geopy.distance import geodesic
# -------------------------
# Create sample city data
# -------------------------
data = {
"City": ["New York", "London", "Tokyo"],
"Latitude": [40.7128, 51.5074, 35.6895],
"Longitude": [-74.0060, -0.1278, 139.6917]
}
df = pd.DataFrame(data)
# -------------------------
# Convert to GeoDataFrame
# -------------------------
geometry = [Point(xy) for xy in zip(df["Longitude"], df["Latitude"])]
gdf = gpd.GeoDataFrame(df, geometry=geometry)
gdf.set_crs(epsg=4326, inplace=True)
# -------------------------
# Static Map
# -------------------------
gdf.plot(color="red", markersize=100)
plt.title("City Locations")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()
# -------------------------
# Interactive Map
# -------------------------
m = folium.Map(location=[20, 0], zoom_start=2)
for index, row in df.iterrows():
folium.Marker(
location=[row["Latitude"], row["Longitude"]],
popup=row["City"]
).add_to(m)
m.save("interactive_map.html")
# -------------------------
# Load World Map
# -------------------------
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.plot(column="pop_est", cmap="OrRd", legend=True)
plt.title("Population by Country")
plt.show()
# -------------------------
# Distance Calculation
# -------------------------
ny = (40.7128, -74.0060)
london = (51.5074, -0.1278)
distance = geodesic(ny, london).km
print("Distance between New York and London:", distance, "km")
# -------------------------
# Export File
# -------------------------
gdf.to_file("cities.shp")