How to Verify if a Lat/Lon Point is Land or Water in Google Maps: Exploring API Options and Workarounds
Have you ever needed to determine if a specific latitude/longitude (lat/lon) coordinate sits on land or water? Whether you’re building a weather app, logistics tool, environmental monitoring system, or outdoor adventure platform, this seemingly simple check is critical for functionality and accuracy.
Google Maps is one of the most widely used mapping platforms, but it doesn’t offer a direct "land vs. water" API endpoint. Instead, developers must leverage existing Google Maps APIs and creative workarounds to infer this information. In this blog, we’ll demystify the process, exploring Google’s API ecosystem, workarounds for gaps, and step-by-step implementation guides to help you reliably detect land or water for any lat/lon point.
Table of Contents#
-
Understanding the Problem: Why Check Land vs. Water?
- Use Cases for Land/Water Detection
- Challenges with Direct Checks
-
Google Maps APIs: Exploring Options
- 2.1 Geocoding API: Reverse Geocoding for Address Clues
- 2.2 Places API: Leveraging Place Types
- 2.3 Elevation API: Indirect Elevation Clues
-
Workarounds: When Direct APIs Fall Short
- 3.1 Combining Google APIs for Enhanced Accuracy
- 3.2 Integrating Third-Party Datasets
- 3.3 Client-Side Checks with Custom Tiles
-
Step-by-Step Tutorial: Detecting Land/Water with Google Geocoding API
- Prerequisites
- Step 1: Get a Google Cloud API Key
- Step 2: Call the Geocoding API Reverse Geocoding Endpoint
- Step 3: Parse the Response for Water-Related Types and Keywords
- Step 4: Handle Edge Cases
-
Limitations and Considerations
- Data Coverage and Accuracy
- Cost Implications
- Rate Limits and Quotas
Understanding the Problem: Why Check Land vs. Water?#
Use Cases for Land/Water Detection#
- Logistics & Shipping: Avoid routing ships through land or trucks through water.
- Weather Apps: Water bodies influence local weather (e.g., lakes moderate temperatures).
- Outdoor Apps: Hikers need to know if a waypoint is on a trail (land) or a lake (water).
- Environmental Studies: Track deforestation, glacial melt, or coastal erosion by monitoring land/water boundaries.
Challenges with Direct Checks#
Google Maps focuses on mapping places (e.g., cities, roads, parks) and routes, not explicit land/water classification. There’s no is_land or is_water field in its core APIs. Instead, we must infer this using indirect signals from address data, place categories, or elevation.
Google Maps APIs: Exploring Options#
Let’s dive into Google’s APIs to see how they can help determine land vs. water.
2.1 Geocoding API: Reverse Geocoding for Address Clues#
The Geocoding API converts lat/lon coordinates into human-readable addresses (reverse geocoding). While not designed for land/water checks, addresses sometimes include clues like "Pacific Ocean" or "Lake Michigan," indicating water.
Example Response for a Water Point:
A coordinate in the Atlantic Ocean (e.g., 30.0, -40.0) might return:
{
"results": [
{
"formatted_address": "Atlantic Ocean",
"types": ["natural_feature", "establishment"]
}
]
} Limitations:
- Many water bodies (e.g., small lakes, rivers) lack explicit "ocean" or "lake" labels in addresses.
- Coastal land areas may return addresses like "Miami Beach, FL" (land) even if near water.
2.2 Places API: Leveraging Place Types#
The Places API returns detailed information about places near a lat/lon, including place types (e.g., "park", "cafe"). However, it does not provide dedicated water body types like "ocean", "lake", or "river". Instead, you can use the Geocoding API for reverse geocoding and check for natural_feature types combined with address keywords to identify water bodies.
2.3 Elevation API: Indirect Elevation Clues#
The Elevation API returns the elevation of a lat/lon point. Water bodies often have elevations near sea level (0 meters), but this is an indirect and unreliable signal:
- Coastal land (e.g., Miami) has elevation ~0m but is land.
- High-altitude lakes (e.g., Lake Titicaca, 3,812m) have high elevation but are water.
- Underwater trenches (e.g., Mariana Trench, -10,928m) are water but have negative elevation.
Use Case: Combine elevation with Geocoding API (e.g., if elevation < -10m and Geocoding returns "Ocean" in the address, confirm water).
Workarounds: When Direct APIs Fall Short#
Google’s APIs aren’t perfect. Here are workarounds to fill gaps:
3.1 Combining Google APIs for Enhanced Accuracy#
For better reliability, combine signals from multiple Google APIs:
- Geocoding API + Elevation API: If Geocoding returns "Ocean" in the address and elevation is near sea level, confidence increases for water detection.
- Geocoding API fallback: If Geocoding returns no results, use elevation < 0m as a backup indicator for water (though this is unreliable).
3.2 Integrating Third-Party Datasets#
If Google’s data is sparse (e.g., remote regions), supplement with third-party data:
- OpenStreetMap (OSM): Use Nominatim (OSM’s geocoder) to fetch land/water tags (e.g.,
natural=water). - NOAA Data: The NOAA Coastal Services Center provides shapefiles for coastlines and water bodies.
- Earth Engine: Google’s Earth Engine (separate from Maps) offers satellite imagery and land cover datasets (e.g., MODIS land cover maps).
3.3 Client-Side Checks with Custom Tiles#
For offline or low-latency needs, use client-side raster tiles:
- Overlay custom tiles (e.g., from Mapbox or OSM) where water is colored blue.
- Use a canvas to check the pixel color at a lat/lon: blue = water, other colors = land.
Example:
Mapbox’s Satellite Streets style uses blue for water. A client-side script can fetch the tile for a lat/lon and sample the pixel color.
Step-by-Step Tutorial: Detecting Land/Water with Google Geocoding API#
Let's implement a land/water check using the Geocoding API, which supports reverse geocoding and can identify water bodies through natural_feature types and address keywords.
Prerequisites#
- A Google Cloud account (sign up here).
- An API key with the Geocoding API enabled (see Google's guide).
- Basic knowledge of HTTP requests and JSON parsing.
Step 1: Get a Google Cloud API Key#
- Go to the Google Cloud Console.
- Create a project, then navigate to APIs & Services > Credentials > Create Credentials > API key.
- Restrict the key to the Geocoding API (under "API restrictions") to avoid misuse.
Step 2: Call the Geocoding API Reverse Geocoding Endpoint#
Use the reversegeocode endpoint to fetch address information for a target lat/lon. The endpoint URL is:
https://maps.googleapis.com/maps/api/geocode/json?latlng=LAT,LON&key=YOUR_API_KEY
Parameters:
latlng: Target coordinates (e.g.,40.7128,-74.0060for New York City).key: Your Google Cloud API key.
Step 3: Parse the Response for Water-Related Types and Keywords#
The API returns a results array with address components. Check the types field for natural_feature and the formatted_address for water-related keywords (e.g., "Ocean", "Lake", "River", "Sea").
Example Code (Python):
import requests
def is_water(lat, lon, api_key):
url = f"https://maps.googleapis.com/maps/api/geocode/json?latlng={lat},{lon}&key={api_key}"
response = requests.get(url).json()
water_keywords = {"ocean", "sea", "lake", "river", "canal", "reservoir", "waterfall"}
for result in response.get("results", []):
# Check types for natural_feature
types = result.get("types", [])
if "natural_feature" in types:
formatted_address = result.get("formatted_address", "").lower()
# Check address for water keywords
if any(keyword in formatted_address for keyword in water_keywords):
return True
return False
# Test with a water point (Atlantic Ocean: 30.0, -40.0)
print(is_water(30.0, -40.0, "YOUR_API_KEY")) # Output: True
# Test with a land point (Eiffel Tower: 48.8584, 2.2945)
print(is_water(48.8584, 2.2945, "YOUR_API_KEY")) # Output: False Step 4: Handle Edge Cases#
- No Results: Remote areas may return no
results. Use a fallback (e.g., assume land, or check elevation). - Mixed Types: A coastal park might return
["park", "natural_feature"](land) but be near water. Prioritize explicit water types.
Limitations and Considerations#
Data Coverage and Accuracy#
- Remote Areas: Google's Geocoding API has limited data in deserts, polar regions, or small islands.
- Small Water Bodies: Ponds or streams may not be identified as water bodies in the API.
Cost Implications#
Google APIs are not free. As of 2024:
- Geocoding API (Reverse Geocoding): ~$0.005 per request (with free tier limits).
- Elevation API: ~$0.005 per 100 requests.
Rate Limits and Quotas#
Google enforces rate limits (e.g., 50 requests per second for Geocoding API). Exceeding limits results in OVER_QUERY_LIMIT errors.
Best Practices for Reliable Detection#
- Use Geocoding API with natural_feature checks: Check for
natural_featuretype combined with address keywords for water detection. - Cache Results: Store land/water status for frequently used coordinates to reduce API calls.
- Combine Signals: Use Elevation API as backup when Geocoding API is ambiguous.
- Test Edge Cases: Validate with known land/water points (e.g., Sahara Desert, Dead Sea).
- Handle Errors Gracefully: Retry on
OVER_QUERY_LIMITor fallback to third-party data. - Consider OpenStreetMap Nominatim: For more comprehensive water body data, use OSM's Nominatim which includes
natural=watertags.
Conclusion#
While Google Maps lacks a direct "land vs. water" endpoint, the Geocoding API (with natural_feature types and address keyword matching) is the most reliable tool for this task using Google's APIs. Combine it with Elevation API for enhanced accuracy, and supplement with third-party data (like OpenStreetMap Nominatim) for remote regions or more comprehensive water body coverage. By following the tutorial and best practices, you can build a robust land/water detection system for your application.