如何使用Python通过坐标调用并显示谷歌地球卫星图像
Hey there, sorry you’ve been banging your head on this for a month—let’s walk through two reliable ways to pull and display Google Earth-style satellite imagery using Python with your target coordinates. I’ve used both approaches for different projects, so they should work if you follow the steps carefully.
This is the simplest way to get a satellite image matching what you’d see in Google Earth, since it pulls directly from the same imagery source.
前提准备
- You’ll need a Google Cloud API key, and you have to enable the Maps Static API in your Google Cloud console (there’s a free tier for low usage, but watch out for overages).
代码示例
import requests from PIL import Image import matplotlib.pyplot as plt # Replace with your own Google Cloud API key API_KEY = "your_actual_api_key_here" # Your target coordinates (latitude, longitude) — example uses San Francisco target_lat, target_lon = 37.7749, -122.4194 # Adjust zoom (1 = global view, 20 = street-level detail; 14-18 works great for city areas) zoom_level = 15 # Image size (max 640x640 for free tier) img_size = "600x600" # Build the request URL for satellite imagery satellite_url = f"https://maps.googleapis.com/maps/api/staticmap?center={target_lat},{target_lon}&zoom={zoom_level}&size={img_size}&maptype=satellite&key={API_KEY}" # Download the image response = requests.get(satellite_url) with open("satellite_shot.jpg", "wb") as img_file: img_file.write(response.content) # Display the image sat_img = Image.open("satellite_shot.jpg") plt.imshow(sat_img) plt.axis('off') # Hide messy axes plt.show()
关键注意点
- The
maptype=satelliteparameter is what triggers the Google Earth-style satellite layer. - Double-check your API key is enabled for Maps Static API—if you get an error, this is the first thing to verify.
If you need access to historical satellite data or want to process imagery beyond just viewing, Earth Engine is the way to go. It’s Google’s platform for geospatial analysis, and it uses the same underlying imagery as Google Earth.
前提准备
- Sign up for a Google Earth Engine account (free for non-commercial use).
- Install the library:
pip install earthengine-api - Authenticate your account by running
earthengine authenticatein your terminal (follow the prompts to log in).
代码示例
import ee import requests from PIL import Image import matplotlib.pyplot as plt # Initialize Earth Engine ee.Initialize() # Your target coordinates (note: Earth Engine uses [longitude, latitude] order!) target_lon, target_lat = -122.4194, 37.7749 # Create a 1km x 1km area around your point (adjust buffer size as needed) target_region = ee.Geometry.Point([target_lon, target_lat]).buffer(500).bounds() # Pull the latest cloud-free Landsat 8 image (matches Google Earth's default satellite layer) satellite_collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') \ .filterBounds(target_region) \ .sort('CLOUD_COVER') \ .first() # Grab the image with the least cloud cover # Select RGB bands to get a true-color image rgb_image = satellite_collection.select(['B4', 'B3', 'B2']) # Generate a thumbnail URL and download the image thumb_url = rgb_image.getThumbURL({ 'region': target_region, 'dimensions': 600, 'format': 'jpg', 'min': 0, 'max': 0.3 # Adjust these values to match Google Earth's brightness }) response = requests.get(thumb_url) with open("ee_satellite.jpg", "wb") as img_file: img_file.write(response.content) # Display the image ee_img = Image.open("ee_satellite.jpg") plt.imshow(ee_img) plt.axis('off') plt.show()
关键注意点
- Earth Engine uses longitude first, latitude second for coordinates—this is a super common gotcha, so don’t mix them up!
- If you want imagery that looks exactly like Google Earth’s default view, try using the
GOOGLE/SATELLITEimage collection instead of Landsat (it’s a static mosaic, so less up-to-date but matches the UI).
- If your API request fails: Check that your key is active, you’ve enabled the correct API, and you haven’t exceeded your quota.
- Earth Engine authentication issues: Re-run
earthengine authenticateand make sure you’re using the same Google account as your Earth Engine registration. - Blurry images: Crank up the zoom level (for Static API) or adjust the
dimensionsparameter (for Earth Engine).
内容的提问来源于stack exchange,提问作者Akash

