如何用R提取Google Maps行政区域边界并计算区域面积?
Great question! You absolutely can extract administrative boundaries like neighborhoods in R, and there are far more reliable ways to calculate their area than counting pixels (which is prone to errors from zoom levels, projection distortions, and manual image processing). Let’s walk through your options:
Option 1: Using Google Maps APIs (Paid, but Precise for Google's Boundaries)
If you want to match the exact boundaries shown in Google Maps, you can use Google's Geocoding/Places APIs with the googleway package. Note that this requires a Google Cloud API key (with billing enabled, though there's a free tier for low usage).
Here’s a quick example:
library(googleway) library(sf) # Set your Google API key (get one from Google Cloud Console) set_key("YOUR_GOOGLE_API_KEY") # Search for your target district to get its Place ID search_results <- google_places(search_string = "Atatürk Mahallesi, 45215 Akhisar/Manisa") target_place_id <- search_results$results$place_id[1] # Fetch detailed place data including boundary geometry place_details <- google_places_details(place_id = target_place_id) # Convert the boundary to an sf spatial object (for easy analysis) district_boundary <- st_as_sf(place_details$result$geometry$location) # Note: For full polygon boundaries, some administrative areas may return a `viewport` or `polygon` field in the geometry—adjust the extraction based on your result structure # Calculate area (transform to a projected CRS first to avoid WGS84 distance errors) district_proj <- st_transform(district_boundary, crs = 32635) # UTM zone 35N for this region area_sqm <- st_area(district_proj) cat("Area:", round(as.numeric(area_sqm)/1000000, 2), "sq km\n")
Option 2: Free & Open Alternative (OpenStreetMap Data)
For a no-cost, open-source solution, use the osmdata package to pull boundaries directly from OpenStreetMap. This is my go-to recommendation because it’s free, doesn’t require API keys, and provides accurate spatial data.
Example code for your target neighborhood:
library(osmdata) library(sf) # Define the OSM query: target neighborhood, admin level (9 = neighborhood in Turkey) district_query <- opq(bbox = "Akhisar, Manisa, Turkey") %>% add_osm_feature(key = "admin_level", value = "9") %>% add_osm_feature(key = "name", value = "Atatürk Mahallesi") # Fetch the spatial data as an sf object district_data <- osmdata_sf(district_query) district_sf <- district_data$osm_polygons # Transform to a projected coordinate system for accurate area calculation district_proj <- st_transform(district_sf, crs = 32635) # UTM 35N matches this location area_sqkm <- as.numeric(st_area(district_proj)) / 1000000 cat("Area of Atatürk Mahallesi:", round(area_sqkm, 2), "square kilometers\n")
Why Pixel Counting Isn’t Ideal
While you could count pixels from a Google Maps screenshot (using packages like magick for image processing), this method has major flaws:
- Projection distortion: Google Maps uses a Mercator projection, which stretches areas near the poles—pixels don’t represent equal real-world area across the map.
- Zoom dependency: The number of pixels per square kilometer changes with zoom level, so you’d need to calculate a pixel-to-area ratio for each screenshot.
- Manual effort: You’d have to manually crop, threshold, and count pixels, which is error-prone and not scalable.
Final Recommendation
Stick with spatial data methods (either Google APIs or OpenStreetMap) for accurate, reproducible area calculations. The osmdata approach is perfect if you want a free, low-hassle solution.
内容的提问来源于stack exchange,提问作者serdaryurek

