Making maps with geographic data | Power Bi Tutorial - Learn with VOKS
Back Next

Making maps with geographic data


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:

  • Points → Cities, schools, restaurants
  • Lines → Roads, rivers
  • Polygons → Countries, states, lakes

Common file types:

  • .shp (Shapefile)
  • .geojson
  • .gpkg

B) Raster Data

Represents data as pixels (like images):

  • Satellite images
  • Elevation maps
  • Weather maps

File types:

  • .tif
  • .jpg
  • .png

2️⃣ Important Geographic Concepts (Simple Explanation)

🌍 Latitude and Longitude

The Earth uses coordinates:

  • Latitude → North/South (−90 to 90)
  • Longitude → East/West (−180 to 180)

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:

  • WGS84EPSG:4326

It uses latitude and longitude.


3️⃣ Tools We’ll Use (Python)

We’ll use:

  • pandas → handles data tables
  • geopandas → handles geographic data
  • matplotlib → plots maps
  • folium → interactive web maps

Install 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 data
  • cmap="OrRd" → color style
  • legend=True → show color scale


8️⃣ Spatial Analysis (Basic Ideas)

Once data is on a map, we can:

  • Measure distance
  • Find nearby locations
  • Count points inside areas
  • Detect patterns

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:

Example Code:
# 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")
Power Bi
Introduction to Power BI Core Features of Power BI Loading and Opening Existing Reports Communicating Key Metrics with Cards Interactivity and Detail — Slicers and Tables Slicers Cleaning Data Power query editor; renaming and re ordering of columns, finding anomalies Field Aggregation & Data Manipulation Transforming & Formatting Columns Formatting Currency Making maps with geographic data Visualization options; dashboards or reports, tables and scatter charts, bubble charts, KPIs, guage Conditional formatting Sorting, Removing Duplicates, and Plotting in Pandas Dax in power bi, context Dax formulas, date data bars, histogram and pie charts Load and Transforming Data Dimensional modeling Facts and dimensional table modeling Breaking tables into multiple tables Finding relationships between tables
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL