地理空间文件格式:shapefile(最常见)、GeoJSON、KML和GPKG
# Read in the data
full_data = gpd.read_file("../input/geospatial-learn-course-data/DEC_lands/DEC_lands/DEC_lands.shp")
# View the first five rows of the data
full_data.head()
坐标参考系由欧洲石油勘探集团(EPSG)规范引用。
这个GeoDataFrame使用的是EPSG 32630,它通常被称为“墨卡托”投影。此投影保留角度(使其对航海有用)并稍微扭曲区域。
但是,当从CSV文件创建GeoDataFrame时,我们必须设置CRS。EPSG 4326对应于经纬度坐标。
# Create a DataFrame with health facilities in Ghana
facilities_df = pd.read_csv("../input/geospatial-learn-course-data/ghana/ghana/health_facilities.csv")
# Convert the DataFrame to a GeoDataFrame
facilities = gpd.GeoDataFrame(facilities_df, geometry=gpd.points_from_xy(facilities_df.Longitude, facilities_df.Latitude))
# Set the coordinate reference system (CRS) to EPSG 4326
facilities.crs = {
'init': 'epsg:4326'}
regions.geometry.length # 线长度
regions.geometry.area # 区域面积
# 1.简单的图层叠加
# Define a base map with county boundaries
ax = counties.plot(figsize=(10,10), color='none', edgecolor='gainsboro', zorder=3)
# Add wild lands, campsites, and foot trails to the base map
wild_lands.plot(color='lightgreen', ax=ax)
campsites.plot(color='maroon', markersize=2, ax=ax)
trails.plot(color='black', markersize=1, ax=ax)
# 2.考虑坐标参考的一致性
ax = regions.plot(figsize=(8,8), color='whitesmoke', linestyle=':', edgecolor='black')
facilities.to_crs(epsg=32630).plot(markersize=1, ax=ax)
相关参数:
location
地图最中心的位置tiles
地图的样式zoom_start
初始的放大倍数,越大越接近原地图# 1.地图背景
m_1 = folium.Map(location=[42.32,-71.0589], tiles='openstreetmap', zoom_start=10)
# Display the map
m_1
# 2.标记图
m_2 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=13)
# Add points to the map
for idx, row in daytime_robberies.iterrows():
Marker([row['Lat'], row['Long']]).add_to(m_2)
# Display the map
m_2
# 3.可变化的标记图
# Create the map
m_3 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=13)
# Add points to the map
mc = MarkerCluster()
for idx, row in daytime_robberies.iterrows():
if not math.isnan(row['Long']) and not math.isnan(row['Lat']):
mc.add_child(Marker([row['Lat'], row['Long']]))
m_3.add_child(mc)
# Display the map
m_3
# 4.圆点图
m_4 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=13)
def color_producer(val):
if val <= 12:
return 'forestgreen'
else:
return 'darkred'
# Add a bubble map to the base map
for i in range(0,len(daytime_robberies)):
folium.Circle(
location=[daytime_robberies.iloc[i]['Lat'], daytime_robberies.iloc[i]['Long']],
radius=20,
color=color_producer(daytime_robberies.iloc[i]['HOUR'])).add_to(m_4)
# Display the map
m_4
# 5.热图
m_5 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=12)
# Add a heatmap to the base map
HeatMap(data=crimes[['Lat', 'Long']], radius=10).add_to(m_5)
# Display the map
m_5
# 6.分级统计图
m_6 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=12)
# Add a choropleth map to the base map
Choropleth(geo_data=districts.__geo_interface__, # 传入geojson
data=plot_dict, # 染色数据区域
key_on="feature.id", # geojson区域id
fill_color='YlGnBu', # 颜色样式
legend_name='Major criminal incidents (Jan-Aug 2018)' # 图例名
).add_to(m_6)
# Display the map
m_6
由于图片未加载完全,所以后面5张图注重效果,不注重背景,请您谅解
地理编码是将一个地方或地址的名称转换为地图上的一个位置的过程,以下是其代码实现
# 基本使用:提供地址名称和地理供应者,得到地址的经纬度和包含完整地址的信息
from geopandas.tools import geocode
result = geocode("The Great Pyramid of Giza", provider="nominatim")
result
实操如下:
# 加载欧洲前100大学的名称
universities = pd.read_csv("../input/geospatial-learn-course-data/top_universities.csv")
def my_geocoder(row):
try:
point = geocode(row, provider='nominatim').geometry.iloc[0]
return pd.Series({
'Latitude': point.y, 'Longitude': point.x, 'geometry': point})
except:
return None
universities[['Latitude', 'Longitude', 'geometry']] = universities.apply(lambda x: my_geocoder(x['Name']), axis=1)
print("{}% of addresses were geocoded!".format(
(1 - sum(np.isnan(universities["Latitude"])) / len(universities)) * 100))
# Drop universities that were not successfully geocoded
universities = universities.loc[~np.isnan(universities["Latitude"])]
universities = gpd.GeoDataFrame(universities, geometry=universities.geometry)
universities.crs = {
'init': 'epsg:4326'}
有关地理坐标的属性连接(信息合并)最好使用gpd.GeoDataFrame.merge()
的方法,如下所示
# 已知欧洲城市地理边界和相关统计信息europe_boundaries.head()和europe_stats.head()
# 对两个数据DataFrame进行信息合并
europe = europe_boundaries.merge(europe_stats, on="name")
europe.head()
空间连接即通过地理空间位置来进行匹配
如使用空间连接来将每个大学与相应的国家匹配,用gpd.sjoin()
的方法,如下所示
# Use spatial join to match universities to countries in Europe
european_universities = gpd.sjoin(universities, europe)
# Investigate the result
print("We located {} universities.".format(len(universities)))
print("Only {} of the universities were located in Europe (in {} different countries).".format(
len(european_universities), len(european_universities.name.unique())))
european_universities.head()
# 测量两点间距离
distances = stations.geometry.distance(recent_release.geometry)
# 创建缓冲区
two_mile_buffer = stations.geometry.buffer(2*5280)