关于arcgis的python脚本编程, shape文件出png图片问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于arcgis的python脚本编程, shape文件出png图片问题相关的知识,希望对你有一定的参考价值。
前提:不使用arcgis的桌面程序, 只使用python来实现
源文件:生成好的shape文件,没有lyr文件,没有mxd文件
问题:arcgis提供的类方法arcpy.mapping.ExportToPNG只能实现通过mxd文件生成png图片的功能,但现在只有shape文件,用arcmap桌面软件实现的方法应该类似于这样:(新建mxd文件,然后添加数据,选择shape文件,最后导出地图成png图片。)现在的问题是,就括号里的流程使用python怎么实现?欢迎实验成功后说下思路,贴上代码。
我的思路:直接用python新建mxd文件,并对改数据框添加图层,图层的源文件为shape文件
碰到的问题:1、直接新建的mxd文件,使用arcpy.mapping.MapDocument无法获取数据框,原因是没有初始化一些必要的配置吗?2、对现有的mxd文件,是否需要先新建lyr图层,并使用lyr.replaceDataSource函数实现添加数据源,怎么使用python实现?
python和arcgis都是刚接触一周多,问题很多。希望耐心解答,别说些自认理所当然的答案,最重要的是代码。
1、点类型的转栅格:
PointToRaster example 1 (Pythonwindow)
Converts point features to a raster dataset.
import arcpy
from arcpy import env
env.workspace = "c:/data"
arcpy.PointToRaster_conversion("ca_ozone_pts.shp", "ELEVATION",
"c:/output/ca_elev", "MAXIMUM", "", 2000)
PointToRaster example 2
(stand-alone script)
Converts point features to a raster dataset.
# Name: PointToRaster_Ex_02.py
# Description: Converts point features to a raster dataset.
# Requirements: ArcInfo
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data"
# Set local variables
inFeatures = "ca_ozone_pts.shp"
valField = "ELEVATION"
outRaster = "c:/output/ca_elev02"
assignmentType = "MAXIMUM"
priorityField = ""
cellSize = 2000
# Execute PointToRaster
arcpy.PointToRaster_conversion(inFeatures, valField, outRaster,
assignmentType, priorityField, cellSize)
2、面类型的转栅格:
PolygonToRaster example 1(Python window)
Converts polygon features to a raster dataset.
import arcpy
from arcpy import env
env.workspace = "c:/data"
arcpy.PolygonToRaster_conversion("ca_counties.shp", "NAME",
"c:/output/ca_counties.img",
"MAXIMUM_AREA", "MALES", 0.25)
PolygonToRaster example 2
(stand-alone script)
Converts polygon features to a raster dataset.
# Name: PolygonToRaster_Ex_02.py
# Description: Converts polygon features to a raster dataset.
# Requirements: ArcInfo
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data"
# Set local variables
inFeatures = "ca_counties.shp"
valField = "NAME"
outRaster = "c:/output/ca_counties"
assignmentType = "MAXIMUM_AREA"
priorityField = "MALES"
cellSize = 0.5
# Execute PolygonToRaster
arcpy.PolygonToRaster_conversion(inFeatures, valField, outRaster,
assignmentType, priorityField, cellSize)
3、线类型转栅格
PolylineToRaster example 1(Python window)
Converts polyline features to a raster dataset.
import arcpy
from arcpy import env
env.workspace = "c:/data"
arcpy.PolylineToRaster_conversion("roads.shp", "CLASS", "c:/output/roads.img",
"MAXIMUM_COMBINED_LENGTH", "LENGTH", 30)
PolylineToRaster example 2
(stand-alone script)
Converts polyline features to a raster dataset.
# Name: PolylineToRaster_Ex_02.py
# Description: Converts polyline features to a raster dataset.
# Requirements: ArcInfo
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data"
# Set local variables
inFeatures = "roads.shp"
valField = "CLASS"
outRaster = "c:/output/roads.tif"
assignmentType = "MAXIMUM_COMBINED_LENGTH"
priorityField = "LENGTH"
cellSize = 30
# Execute PolylineToRaster
arcpy.PolylineToRaster_conversion(inFeatures, valField, outRaster,
assignmentType, priorityField, cellSize)追问
就只是使用了工具箱生成的python代码吗?
1、你自己有没有试过生成的python能不能运行?
2、我要生成的是mxd文件,然后导出png文件,并不需要栅格文件
咱只是提供点线索。
你可以试试,毕竟每个人都有自己的事,如果恰好做过,而且热心的人肯帮助你那是你运气好。祝你好运。
pip安装shap报错
python pip install shap 报错 error ERROR: Command errored out with exit status 1:
linux pip安装shap 报错 Building wheel for shap (setup.py) … error ERROR: Command errored out with exit status 1: command: /usr/python/anaconda3/bin/python -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"’"’/tmp/pip-install-g0yvh0xs/shap_cdc81c9cd7784671b7edc8e56d244549/setup.py’"’"’; file=’"’"’/tmp/pip-install-g0yvh0xs/shap_cdc81c9cd7784671b7edc8e56d244549/setup.py’"’"’;f=getattr(tokenize, ‘"’"‘open’"’"’, open)(file);code=f.read().replace(’"’"’\\r\\n’"’"’, ‘"’"’\\n’"’"’);f.close();exec(compile(code, file, ‘"’"‘exec’"’"’))’ bdist_wheel -d /tmp/pip-wheel-y3wnvdej cwd: /tmp/pip-install-g0yvh0xs/shap_cdc81c9cd7784671b7edc8e56d244549/ error: command ‘gcc’ failed with exit status 1
原因:
缺少cpp
解决:
安装cpp
> sudo apt install build-essential # ubuntu
> sudo yum install build-essential # centos
结果:
> pip install shap
如果这篇文章对你有用,请点个赞吧....
以上是关于关于arcgis的python脚本编程, shape文件出png图片问题的主要内容,如果未能解决你的问题,请参考以下文章
Python编程学习:深度剖析shap.datasets.adult()源码中的X,y和X_display,y_display输出数区别