使用geopandas和matplotlib绘制地图 [英] Plotting a map using geopandas and matplotlib

查看:417
本文介绍了使用geopandas和matplotlib绘制地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型的csv,具有来自英国伯明翰的6个坐标.我用熊猫阅读了csv,然后将其转换为GeoPandas DataFrame,并使用Shapely Points更改了我的纬度和经度列.我现在正在尝试绘制我的GeoDataframe,我所能看到的就是要点.如何获得伯明翰地图?一个很好的有关GeoPandas的文档来源也将受到高度赞赏.

I have small csv that has 6 coordinates from Birmingham England. I read the csv with pandas then transformed it into GeoPandas DataFrame changing my latitude and longitude columns with Shapely Points. I am now trying to plot my GeoDataframe and all I can see are the points. How do I get the Birmingham map represented as well? A good documentation source on GeoPandas would be strongly appreciated too.

from shapely.geometry import Point
import geopandas as gpd
import pandas as pd

df = pd.read_csv('SiteLocation.csv')
df['Coordinates'] = list(zip(df.LONG, df.LAT))
df['Coordinates'] = df['Coordinates'].apply(Point)
# Building the GeoDataframe 
geo_df = gpd.GeoDataFrame(df, geometry='Coordinates')
geo_df.plot()  

推荐答案

GeoPandas文档包含有关如何向地图添加背景的示例(

The GeoPandas documentation contains an example on how to add a background to a map (https://geopandas.readthedocs.io/en/latest/gallery/plotting_basemap_background.html), which is explained in more detail below.

您将不得不处理瓷砖,即通过网络投放的(png)图像服务器,URL类似

You will have to deal with tiles, that are (png) images served through a web server, with a URL like

http://.../Z/X/Y.png,其中Z是缩放级别,X和Y标识图块

http://.../Z/X/Y.png, where Z is the zoom level, and X and Y identify the tile

geopandas的文档显示了如何将图块设置为绘图的背景,如何获取正确的图块以及完成空间同步等所有其他原本困难的工作...

And geopandas's doc shows how to set tiles as backgrounds for your plots, fetching the correct ones and doing all the otherwise difficult job of spatial syncing, etc...


安装


Installation

假设已经安装了GeoPandas,则还需要 contextily 软件包.如果您在Windows下,则可能要看一下如何进行上下文安装?

Assuming GeoPandas is already installed, you need the contextily package in addition. If you are under windows, you may want to pick a look at How to install Contextily?

用例

Use case

创建一个python脚本,然后定义 上下文帮助功能

Create a python script and define the contextily helper function

import contextily as ctx

def add_basemap(ax, zoom, url='http://tile.stamen.com/terrain/tileZ/tileX/tileY.png'):
    xmin, xmax, ymin, ymax = ax.axis()
    basemap, extent = ctx.bounds2img(xmin, ymin, xmax, ymax, zoom=zoom, url=url)
    ax.imshow(basemap, extent=extent, interpolation='bilinear')
    # restore original x/y limits
    ax.axis((xmin, xmax, ymin, ymax))

import matplotlib.pyplot as plt
from shapely.geometry import Point
import geopandas as gpd
import pandas as pd

# Let's define our raw data, whose epsg is 4326
df = pd.DataFrame({
    'LAT'  :[-22.266415, -20.684157],
    'LONG' :[166.452764, 164.956089],
})
df['coords'] = list(zip(df.LONG, df.LAT))

# ... turn them into geodataframe, and convert our
# epsg into 3857, since web map tiles are typically
# provided as such.
geo_df = gpd.GeoDataFrame(
    df, crs  ={'init': 'epsg:4326'},
    geometry = df['coords'].apply(Point)
).to_crs(epsg=3857)

# ... and make the plot
ax = geo_df.plot(
    figsize= (5, 5),
    alpha  = 1
)
add_basemap(ax, zoom=10)
ax.set_axis_off()
plt.title('Kaledonia : From Hienghène to Nouméa')
plt.show()


注意:您可以使用zoom来找到地图的高分辨率. :


Note: you can play with the zoom to find the good resolution for the map. E.g./I.e. :

...,并且此类分辨率隐式要求更改x/y限制.

这篇关于使用geopandas和matplotlib绘制地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆