---
updatedAt: 2025-12-16T13:18:45.000Z
agentTools:
  projectIndex: https://docs.getfastah.com/llms.txt
---

# Maps and Geolocation Data

Plot IP geolocation data on an interactive map for easy visualization.

If you have server logs with 100+ entries and want to visualize where requests originated geographically, follow these steps:

### Extract IP addresses from log entries

Assuming Apache Common Log Format, where the first field is the client IP address:

```python
ip_addresses = []
# Populate an array with IPs of interest
with open('access.log', 'r') as file:
    for line in file:
        ip = line.split()[0]
        ip_addresses.append(ip)
```

### Query the Fastah API and build an IP-to-coordinates dictionary

For each unique IP, fetch geolocation data and store the results:

```python
ip_location_dictionary = {}

# Query Fastah API for each IP and build a dictionary of IP addresses to coordinates
for ip in ip_addresses:
    if ip not in ip_location_dictionary.get("ip",""):
        # Build API request
        url = f"https://ep.api.getfastah.com/whereis/v1/json/{ip}"
        headers = {"Fastah-Key": FASTAH_API_KEY}

        # Fetch geolocation data
        response = requests.get(url, headers=headers)
        data = response.json()

        # Extract coordinates from response
        ip_address = data["ip"]
        latitude = data["locationData"]["lat"]
        longitude = data["locationData"]["lng"]
        ip_location_dictionary[ip_address] = {"latitude": latitude, "longitude": longitude}
```

### Render markers on an interactive map

Use the <Anchor label="folium" target="_blank" href="https://pypi.org/project/folium/">folium</Anchor> Python library that wraps Leaflet.js maps to create clustered markers. Or substitute <Anchor label="Google Maps" target="_blank" href="https://developers.google.com/maps/apis-by-platform">Google Maps</Anchor> or <Anchor label="Mapbox" target="_blank" href="https://docs.mapbox.com/api/guides/">Mapbox</Anchor>).

```python
# Initialize map centered at world view with zoom level 2
geolocation_map = folium.Map(location=[0, 0], zoom_start=2)

# Add marker cluster group to organize multiple location points
marker_cluster = MarkerCluster().add_to(geolocation_map)

# Add individual markers for each IP location
for ip_address, coordinates in ip_location_dictionary.items():
    folium.Marker(
        location=[coordinates["latitude"], coordinates["longitude"]],
        popup=ip_address
    ).add_to(marker_cluster)

# Save interactive map to HTML file
geolocation_map.save("ip_map.html")
```

<br />