ncl如何添加线shp文件_NCL画图个例讲解
时间: 2024-06-08 08:10:39 浏览: 124
NCL中可以使用add_shapefile函数来添加线shp文件,具体操作如下:
1.导入NCL库和数据:
```python
import Ngl,Nio
#读取shp数据
f = Nio.open_file("./data/lines.shp","r")
shp = Ngl.shapefile_read(f)
```
2.设置绘图参数:
```python
#设置绘图参数
res = Ngl.Resources()
res.nglDraw = False
res.nglFrame = False
res.wkColorMap = "nrl_spectral"
res.mpProjection = "Orthographic"
res.mpDataBaseVersion = "MediumRes"
res.mpOutlineBoundarySets = "National"
res.mpNationalLineColor = "black"
res.mpLandFillColor = "white"
res.mpOceanFillColor = "lightsteelblue"
res.mpInlandWaterFillColor = "lightsteelblue"
res.mpGridAndLimbOn = False
res.mpGeophysicalLineThicknessF = 4.0
res.mpNationalLineThicknessF=4.0
res.mpInlandWaterLineThicknessF=4.0
res.mpOutlineSpecifiers="China:states"
```
3.添加shp文件:
```python
#添加shp文件
Ngl.add_shapefile(wks, res, shp)
```
4.绘制地图:
```python
#绘制地图
map = Ngl.map(wks, res)
```
完整代码如下:
```python
import Ngl,Nio
#读取shp数据
f = Nio.open_file("./data/lines.shp","r")
shp = Ngl.shapefile_read(f)
#打开一个绘图工作区
wks = Ngl.open_wks("png","test")
#设置绘图参数
res = Ngl.Resources()
res.nglDraw = False
res.nglFrame = False
res.wkColorMap = "nrl_spectral"
res.mpProjection = "Orthographic"
res.mpDataBaseVersion = "MediumRes"
res.mpOutlineBoundarySets = "National"
res.mpNationalLineColor = "black"
res.mpLandFillColor = "white"
res.mpOceanFillColor = "lightsteelblue"
res.mpInlandWaterFillColor = "lightsteelblue"
res.mpGridAndLimbOn = False
res.mpGeophysicalLineThicknessF = 4.0
res.mpNationalLineThicknessF=4.0
res.mpInlandWaterLineThicknessF=4.0
res.mpOutlineSpecifiers="China:states"
#添加shp文件
Ngl.add_shapefile(wks, res, shp)
#绘制地图
map = Ngl.map(wks, res)
#绘制标题
txres = Ngl.Resources()
txres.txFontHeightF = 0.025
Ngl.text_ndc(wks,"Lines Shapefile Example",0.5,0.9,txres)
#输出图像
Ngl.frame(wks)
Ngl.end()
```
运行代码后会生成一张带有线shp文件的地图,示例图如下:
![lines_shapefile_example](https://user-images.githubusercontent.com/57784077/137056682-00ee1d8f-7e3e-4d5e-8b7f-f581b99f3dc7.png)
阅读全文