将geojson转为图片
时间: 2023-09-13 16:07:25 浏览: 413
可以使用Python中的Pillow和GeoJSON库来将GeoJSON转换为图片。以下是一个示例代码:
```python
from PIL import Image, ImageDraw
import json
from io import BytesIO
import requests
import geojson
url = "https://example.com/your_geojson_file.geojson"
response = requests.get(url)
data = json.loads(response.content)
# Get bounding box coordinates
min_lon, min_lat, max_lon, max_lat = geojson.utils.coords(data)
# Calculate image size
width, height = 1024, int(1024 * ((max_lat - min_lat) / (max_lon - min_lon)))
# Create image
img = Image.new("RGB", (width, height), color='white')
draw = ImageDraw.Draw(img)
# Convert geojson to image coordinates
coords = []
for feature in data['features']:
geometry = feature['geometry']
if geometry['type'] == 'Polygon':
for polygon in geometry['coordinates']:
xys = []
for lon, lat in polygon:
x = (lon - min_lon) / (max_lon - min_lon) * width
y = height - (lat - min_lat) / (max_lat - min_lat) * height
xys.append((x, y))
coords.append(xys)
elif geometry['type'] == 'LineString':
xys = []
for lon, lat in geometry['coordinates']:
x = (lon - min_lon) / (max_lon - min_lon) * width
y = height - (lat - min_lat) / (max_lat - min_lat) * height
xys.append((x, y))
coords.append(xys)
# Draw geojson on image
for coord in coords:
draw.polygon(coord, outline='black')
# Save image
img.save('your_output_image.png')
```
请将上述代码按照实际情况进行修改,并确保您已经安装了Pillow和GeoJSON库。
阅读全文