Kaggle-geospatial-analysis(2)

Coordinate-reference-systems

您在本课程中创建的地图以二维方式描绘了地球表面。 但是,正如您所知,世界实际上是三维地球。 因此,我们必须使用一种称为地图投影的方法将其渲染为平坦表面。
地图投影不能100%准确。 每个投影都会以某种方式扭曲地球表面,同时保留一些有用的属性。 例如,
等面积投影(例如“兰伯特圆柱等分面积”或“非洲阿尔伯斯等分圆锥”)可保留面积。

例如,如果您想计算一个国家或城市的面积,这是一个不错的选择。
等距投影(例如“方位角等距投影”)保留距离。 这将是计算飞行距离的好选择。

 

 

 我们使用坐标参考系统(CRS)来显示投影点如何对应于地球上的真实位置。 在本教程中,您将了解有关坐标参考系统的更多信息,以及如何在GeoPandas中使用它们。

 

坐标参考系统由欧洲石油测量集团(EPSG)代码参考。
此GeoDataFrame使用EPSG 32630,通常更称为“墨卡托”投影。 该投影保留了角度(使其对于海上航行很有用)并且使区域略微变形。
但是,从CSV文件创建GeoDataFrame时,必须设置CRS。 EPSG 4326对应于纬度和经度的坐标s

Setting the CRS

当我们从shapefile创建GeoDataFrame时,已经为我们导入了CRS。

# Load a GeoDataFrame containing regions in Ghana
regions = gpd.read_file("../input/geospatial-learn-course-data/ghana/ghana/Regions/Map_of_Regions_in_Ghana.shp")
print(regions.crs)

Output:

{'init': 'epsg:32630'}

坐标参考系统由欧洲石油测量集团(EPSG)代码参考。
此GeoDataFrame使用EPSG 32630,通常更称为“墨卡托”投影。 该投影保留了角度(使其对于海上航行很有用)并且使区域略微变形
但是,从CSV文件创建GeoDataFrame时,必须设置CRS。 EPSG 4326对应于纬度和经度的坐标,试例如下:

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'}

# View the first five rows of the GeoDataFrame
facilities.head()

在上面的代码单元中,要从CSV文件创建GeoDataFrame,我们需要同时使用Pandas和GeoPandas:
1.我们首先创建一个DataFrame,其中包含具有经度和纬度坐标的列。
2.要将其转换为GeoDataFrame,我们使用gpd.GeoDataFrame()。
3.gpd.points_from_xy()函数从纬度和经度列创建Point对象。

Re-projecting

重新投影是指更改CRS的过程。 这是在GeoPandas中使用to_crs()方法完成的。
绘制多个GeoDataFrame时,重要的是它们都使用相同的CRS。 在下面的代码单元中,我们在绘制之前更改设施GeoDataFrame的CRS以匹配区域的CRS。

# Create a map
ax = regions.plot(figsize=(8,8), color='whitesmoke', linestyle=':', edgecolor='black')
facilities.to_crs(epsg=32630).plot(markersize=1, ax=ax)

 

 

 to_crs()方法仅修改“ geometry”列:所有其他列均保持不变。

 

Attributes of geometric objects

正如您在第一个教程中所了解的那样,对于任意的GeoDataFrame,“ geometry”列中的类型取决于我们要显示的内容:例如,我们可以使用:

震中点
街道的LineString,或
显示国家边界的多边形。
三种类型的几何对象均具有内置属性,可用于快速分析数据集。 例如,您可以分别从x和y属性获得Point的x和y坐标。

 

练习

1.Load the data.

There are 11 birds in the dataset, where each bird is identified by a unique value in the "tag-local-identifier" column. Each bird has several measurements, collected at different times of the year.

Use the next code cell to create a GeoDataFrame birds.

  • birds should have all of the columns from birds_df, along with a "geometry" column that contains Point objects with (longitude, latitude) locations.
  • Set the CRS of birds to {'init': 'epsg:4326'}.
# Your code here: Create the GeoDataFrame
birds = gpd.GeoDataFrame(birds_df, geometry=gpd.points_from_xy(birds_df["location-long"], birds_df["location-lat"]))

# Your code here: Set the CRS to {'init': 'epsg:4326'}
birds.crs = {'init': 'epsg:4326'}

                                                                     
# Check your answer
q_1.check()

2. Plot the data.

Next, we load in the 'naturalearth_lowres' dataset from GeoPandas, and set americas to a GeoDataFrame containing the boundaries of all countries in the Americas (both North and South America). Run the next code cell without changes.

# Load a GeoDataFrame with country boundaries in North/South America, print the first 5 rows
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
americas = world.loc[world['continent'].isin(['North America', 'South America'])]
americas.head()

Use the next code cell to create a single plot that shows both: (1) the country boundaries in the americas GeoDataFrame, and (2) all of the points in the birds_gdf GeoDataFrame.

Don't worry about any special styling here; just create a preliminary plot, as a quick sanity check that all of the data was loaded properly. In particular, you don't have to worry about color-coding the points to differentiate between birds, and you don't have to differentiate starting points from ending points. We'll do that in the next part of the exercise.

 

posted @ 2020-08-04 17:56  caishunzhe  阅读(256)  评论(0编辑  收藏  举报