# 地圖視覺化_認識套件
______________
## numpy
用於科學計算
- Sample code:
1. 創陣列
```python=
import numpy as np
# 創建一個一維陣列
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1)
# 創建一個二維陣列
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2)
```
2. 數學運算
```python=
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# 求和
print(np.sum(arr))
# 求平均值
print(np.mean(arr))
# 求最大值
print(np.max(arr))
```
3. 數組操作
```python=
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# 獲取第三個元素(陣列由0開始算,因此輸入arr[2])
print(arr[2])
# 切片
print(arr[1:4])
```
____________
## pyshp
pyshp 是 Python 的一個用於讀取和寫入 Shapefile 文件的庫。Shapefile 是一種常見的地理信息系統(GIS)數據格式,用於存儲地理空間數據,如點、線、多邊形等地理要素。
- Sample code:
1. 讀取shp檔
```python=
import shapefile
# 打開 Shapefile 文件
sf = shapefile.Reader("path_to_shapefile.shp")
# 獲取 Shapefile 文件中的所有記錄
shapes = sf.shapes()
records = sf.records()
# 獲取字段名稱
fields = sf.fields
field_names = [field[0] for field in fields[1:]]
```
2. 獲取要素幾何訊息
```python=
# 獲取第一個要素的幾何信息
shape = shapes[0]
# 獲取第一個要素的屬性信息
record = records[0]
```
3. 創建shp
```python=
import shapefile
# 創建一個 Shapefile 寫入器
w = shapefile.Writer("new_shapefile")
# 添加字段
w.field("Name", "C")
w.field("Value", "N")
# 添加要素
w.point(1, 1)
w.record("Point 1", 123)
# 保存 Shapefile 文件
w.save()
```
_____________________
## geopandas
Geopandas 是建立在 Pandas 基礎上的套件,主要用於地理空間數據處理,可用於讀取Shapefile
* Sample code:
```python=
import geopandas as gpd
import matplotlib.pyplot as plt
# 讀取 Shapefile 文件(shp)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# 繪製地圖
world.plot()
# 顯示地圖
plt.show()
```
:::info
#### 腦力激盪
1. 本次教學中,下載geopandas套件的指令為何?