Geopandas
可以讀取形狀檔, 並包含兩個來志Natural Earth 的檔案: 國家/大陸輪廓,以及國家首都城市
import geopandas
import matplotlib.pyplot as plt
world_file=geopandas.datasets.get_path('naturalearth_lowres')
#使用geopandas.datasets.get_path()函式來獲取
#'naturalearth_lowres'的地理空間數據的文件路徑,
#該數據集包含全球國家和地區邊界。
world = geopandas.read_file(world_file)
#這裡使用geopandas.read_file()函式讀取並解析地理空間數據文件,
#然後將它存在world變數中
#world是一個GeoDataFrame對象,包含國家和地區的邊界多型和相關屬性
print(world,type(world))
cities_file = geopandas.datasets.get_path('naturalearth_cities')
#使用geopandas.datasets.get_path()獲取'naturalearth_cities'的
#地理空間數據文件路徑,該數據集包含全球城市的位置
cities = geopandas.read_file(cities_file)
#geopandas.read_file()讀取並解析地理空間數據文件,存在cities變數中
base = world.plot(color = 'orchid')
#使用world.plot() 繪製地圖的底圖 ,並指定顏色為orchid
cities.plot(ax=base, color='black', markersize=2)
#這邊式繪製成式的位置 ax=base表示城市位置繪製在之前繪製的地圖上
#markersize=2 指定城市點的大小
plt.show()
#顯示繪製好的地圖
https://www.naturalearthdata.com/downloads/110m-cultural-vectors/
可以到這個網頁抓取形狀檔 到要下載的圖像
import geopandas as gpd
import matplotlib.pyplot as plt
usa_file=gpd.read_file("ne_110m_admin_1_states_provinces.shp")
#讀取並解析.shp 地理空間數據集
usa_water_file=gpd.read_file('.//water_line//ne_110m_admin_1_states_provinces_lakes.shp')
#讀取並解析.shp 地理空間數據集
base=usa_file.plot(color='g')
#設定州的顏色是綠色
usa_water_file.plot(ax=base,color='b')
#繪製在州的基礎上 設定湖的顏色是藍色
plt.show()
-------------補充 下載的壓縮檔內
'ne_110m_admin_1_states_provinces.cpg', 'ne_110m_admin_1_states_provinces.dbf', 'ne_110m_admin_1_states_provinces.prj', 'ne_110m_admin_1_states_provinces.README.html', 'ne_110m_admin_1_states_provinces.sbn', 'ne_110m_admin_1_states_provinces.sbx', 'ne_110m_admin_1_states_provinces.shp', 'ne_110m_admin_1_states_provinces.shx', 'ne_110m_admin_1_states_provinces.VERSION.txt', 'ne_110m_admin_1_states_provinces.zip'
大致上有這些資料
.shp是形狀像輛資訊
.shx是形狀索引
.dbf是屬性資料庫
在程式碼中 使用geopandas.read_file()函式讀取.shp文件時,Geopandas會自動讀取相應的
.dbf、.prj、和.shx文件來建構GeoDataFrame 這些文件式Shapefile必要構成的部分,因此只需要指定.shp文件路徑即可,其他文件只需要確保與.shp放在同一目錄下即可
標籤: 練習
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁