import geopandas as gpd
import arcpy
from arcpy import env
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
from mpl_toolkits.basemap import Basemap
from matplotlib_scalebar.scalebar import ScaleBar
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']# 替换sans-serif字体为黑体
plt.rcParams['axes.unicode_minus'] = False # 解决坐标轴负数的负号显示问题
path = r'F:\ArcGIS\ArcGIS文件\ModelBuilder\data\chp05'
env.workspace = path
par_path = path+'\宗地.shp'
floor_path = path + '\楼层数.shp'
## 定义投影
## 都建议大家这么写,有异常捕捉
try:
# set local variables
in_dataset = par_path #"forest.shp"
# get the coordinate system by describing a feature class
dsc = arcpy.Describe(floor_path)
coord_sys = dsc.spatialReference
# run the tool
arcpy.DefineProjection_management(in_dataset, coord_sys)
# print messages when the tool runs successfully
print(arcpy.GetMessages(0))
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
except Exception as ex:
print(ex.args[0])
## 如何叠图,关键ax = ax
fig, ax = plt.subplots(figsize=(8, 8))
floor['coords'] = floor['geometry'].apply(lambda x: x.representative_point().coords[0])
for n, i in enumerate(floor['coords']):
plt.text(i[0], i[1], floor['floor'][n], size=20)
parcels.plot(ax=ax,alpha=0.3, edgecolor='k',label='宗地')
floor.plot(ax=ax,label='大楼')
#plt.savefig('待处理.png',dpi=300)
## 就是开头的图片,这里就不演示了
## arcpy获取字段
featureclass = par_path
field_names = [f.name for f in arcpy.ListFields(featureclass)]
field_names
## 删除文件,我做过一遍的
in_data = 'F:\ArcGIS\ArcGIS文件\ModelBuilder\data\chp05\楼层数_SpatialJoin.shp'
try:
#arcpy.Delete_management (in_data)
# print messages when the tool runs successfully
print(arcpy.GetMessages(0))
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
except Exception as ex:
print(ex.args[0])
## 空间连接
try:
# set local variables
target_features = 'F:\ArcGIS\ArcGIS文件\ModelBuilder\data\chp05\楼层数.shp'
join_features = 'F:\ArcGIS\ArcGIS文件\ModelBuilder\data\chp05\宗地.shp'
out_feature_class = 'F:\ArcGIS\ArcGIS文件\ModelBuilder\data\chp05\楼层数_SpatialJoin.shp'
field_mapping = ['floor','宗地号']
# run the tool
arcpy.SpatialJoin_analysis(target_features, join_features, out_feature_class,)
# print messages when the tool runs successfully
print(arcpy.GetMessages(0))
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
except Exception as ex:
print(ex.args[0])
## 删除字段
f_Spat = 'F:\ArcGIS\ArcGIS文件\ModelBuilder\data\chp05\楼层数_SpatialJoin.shp'
try:
featureclass = 'F:\ArcGIS\ArcGIS文件\ModelBuilder\data\chp05\楼层数_SpatialJoin.shp'
field_names = [f.name for f in arcpy.ListFields(featureclass)]
field_names
del_features = ['Text','街坊号','宗地预','Sum_占地','Sum_建筑','土地用','登记号','权利人',
'宗地类','土地坐','面积','土地证','权属性','使用权','宗地四','宗地面']
arcpy.DeleteField_management(f_Spat,del_features )
# print messages when the tool runs successfully
print(arcpy.GetMessages(0))
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
except Exception as ex:
print(ex.args[0])
表面上没有什么变化,但是结果属性表多了宗地号,还有其他两个字段,接下来可视化一下。
注意
第21行语句如果用“w”,则只会保存一条信息
f = open("F:\code\输出目标文件夹下图片信息\OutPixel.txt", "a+")
“w” 只能操作写入,“r” 只能读取,“a” 向文件追加
“w+” 可读可写,"r+"可读可写,"a+"可读可追加
"wb+"写入进制数据
"w"模式打开文件,如果文件中有数据,再次写入内容,会把原来的覆盖掉
打开txt文件时,如有中文需要使用gbk