提取特定关键字数据
import pandas as pd
df=pd.read_csv("POI.csv",sep=',',dtype={'userid':str,'time':str,"Lat": str, "Lon": str})
df=df[df['type'].str.contains("小区")]
df.to_csv('小区.csv')
POI重分类
import pandas as pd
df=pd.read_csv("POI.csv",sep=',',dtype={'userid':str,'time':str,"Lat": str, "Lon": str})
df['type_news']='其他'
df.loc[df['type'].str.contains("超级市场"),'type_news']="商业"
df.loc[df['type'].str.contains("购物中心"),'type_news']="商业"
df.loc[df['type'].str.contains("综合市场"),'type_news']="商业"
df.loc[df['type'].str.contains("展览馆"),'type_news']="文化"
df.loc[df['type'].str.contains("博物馆"),'type_news']="文化"
df.loc[df['type'].str.contains("音乐厅"),'type_news']="文化"
df.loc[df['type'].str.contains("会展中心"),'type_news']="文化"
df.loc[df['type'].str.contains("美术馆"),'type_news']="文化"
df.loc[df['type'].str.contains("图书馆"),'type_news']="文化"
df.loc[df['type'].str.contains("科技馆"),'type_news']="文化"
df.loc[df['type'].str.contains("文化宫"),'type_news']="文化"
df.loc[df['type'].str.contains("档案馆"),'type_news']="文化"
df.loc[df['type'].str.contains("科技馆"),'type_news']="文化"
df.loc[df['type'].str.contains("天文馆"),'type_news']="文化"
df.loc[df['type'].str.contains("公园"),'type_news']="自然"
df.loc[df['type'].str.contains("自然地名"),'type_news']="自然"
df.loc[df['type'].str.contains("综合医院"),'type_news']="医院"
df.to_csv('POI.csv')
处理POI
1,按类别处理
2,提取各小区点2000米内的要素数量
import arcpy
import arcpy.mp as map
out_path=r"D:\数据处理"
try:
#打开工程文件
aprx=map.ArcGISProject("CURRENT")
Plist=['公交','地铁','购物服务','科教文化服务','银行','公园广场','自然地名','医疗保健服务','中学','高等院校']
for P in Plist:
print(P)
#清空小区选择
arcpy.SelectLayerByAttribute_management("小区", "CLEAR_SELECTION")
#选择目标POI
arcpy.SelectLayerByAttribute_management("POI", "NEW_SELECTION","type LIKE '%"+P+"%'")
#创建表格,写入表头
fo = open(out_path+"\\小区附近的"+P+'数量.csv','w',encoding ='utf-8')
fo.writelines('ID,name,Lon,Lat,Num,NEAR_DIST\n')
fo.close()
for Hi in range(0,2052):
#选择目标小区,完成循环后必须清空选择,否则下一轮邻近计算只有一个小区参与
select_Home=arcpy.SelectLayerByAttribute_management("小区", "NEW_SELECTION",'"ID" = '+str(Hi))
#选择在小区2000米内的要素
select_Poi=arcpy.SelectLayerByLocation_management('POI', 'WITHIN_A_DISTANCE_GEODESIC','小区' , '2000 Meters',"SUBSET_SELECTION")
#选择的数量
select_Num=select_Poi[2]
#写入选择的数据
fo = open(out_path+"\\小区附近的"+P+'数量.csv','a',encoding ='utf-8')
cursor = arcpy.SearchCursor(select_Home)
for row in cursor:
line=str(row.getValue('ID'))+','+row.getValue('name')+','+str(row.getValue('Lon'))\
+','+str(row.getValue('Lat'))+','+select_Num)
fo.writelines(line+'\n')
fo.close()
#选择目标POI
arcpy.SelectLayerByAttribute_management("POI", "NEW_SELECTION","type LIKE '%"+P+"%'")
except Exception as e:
print("ERROR:"+e.args[0])
3,计算各小区到要素的最近距离
GEODESIC 要素之间将使用测地线距离。 这种方法考虑到椭球体的曲率,并可以正确处理日期变更线和两极附近的数据。
out_path=r"D:\数据处理"
Plist=['公交','地铁','购物服务','科教文化服务','银行','公园广场','自然地名','医疗保健服务','中学','高等院校']
for P in Plist:
#清空小区选择
arcpy.SelectLayerByAttribute_management("小区", "CLEAR_SELECTION")
#选择目标POI
arcpy.SelectLayerByAttribute_management("POI", "NEW_SELECTION","type LIKE '%"+P+"%'")
#计算最近邻数据
arcpy.analysis.Near("小区", "POI", "2000 Meters", "NO_LOCATION", "NO_ANGLE", "GEODESIC", "NEAR_DIST NEAR_DIST")
#创建表格,写入表头
fo = open(out_path+"\\小区附近的"+P+'距离.csv','w',encoding ='utf-8')
fo.writelines('ID,name,Lon,Lat,NEAR_DIST\n')
fo.close()
#写入选择的数据
fo = open(out_path+"\\小区附近的"+P+'距离.csv','a',encoding ='utf-8')
cursor = arcpy.SearchCursor('小区')
for row in cursor:
line=str(row.getValue('ID'))+','+row.getValue('name')+','+str(row.getValue('Lon'))\
+','+str(row.getValue('Lat'))+','+str(row.getValue('NEAR_DIST'))
fo.writelines(line+'\n')
fo.close()
3,最终代码
import arcpy
import arcpy.mp as map
out_path=r"C:\Users\Administrator\Desktop\MyProject1\data"
try:
#打开工程文件
aprx=map.ArcGISProject("CURRENT")
Plist=['公交站','中学', '自然', '地铁','高校', '购物中心', '文化', '小学', '医院', '银行']
Distance=[500,1000,1000,1500,1000,1000,1000,1000,2000,500]
for i in range(len(Plist)):
P=Plist[i]
D=str(Distance[i])
print(P+"-"+D)
#清空小区选择,完成循环后必须清空选择,否则下一轮邻近计算只有一个小区参与
arcpy.SelectLayerByAttribute_management("小区", "CLEAR_SELECTION")
#选择目标POI
arcpy.SelectLayerByAttribute_management("POI", "NEW_SELECTION","type_news LIKE '%"+P+"%'")
#创建表格,写入表头
fo = open(out_path+"\\小区附近的"+P+"-"+D+'.csv','w',encoding ='utf-8')
fo.writelines('ID,name,Lon,Lat,Num,NEAR_DIST\n')
fo.close()
#计算最近邻数据
arcpy.analysis.Near("小区", "POI", D+" Meters", "NO_LOCATION", "NO_ANGLE", "GEODESIC")
for Hi in range(1,2435):
#初始化目标POI选择
arcpy.SelectLayerByAttribute_management("POI", "NEW_SELECTION","type_news LIKE '%"+P+"%'")
#选择第Hi个小区
select_Home=arcpy.SelectLayerByAttribute_management("小区", "NEW_SELECTION",'"ID" = '+str(Hi))
#选择在Hi小区D米内的要素
select_Poi=arcpy.SelectLayerByLocation_management('POI', 'WITHIN_A_DISTANCE_GEODESIC','小区' , D+' Meters',"SUBSET_SELECTION")
#选择的数量
select_Num=select_Poi[2]
#写入选择的数据
fo = open(out_path+"\\小区附近的"+P+"-"+D+'.csv','a',encoding ='utf-8')
cursor = arcpy.SearchCursor(select_Home)
for row in cursor:
line=str(row.getValue('ID'))+','+row.getValue('name')+','+str(row.getValue('Lon'))\
+','+str(row.getValue('Lat'))+','+select_Num+','+str(row.getValue('NEAR_DIST'))
fo.writelines(line+'\n')
fo.close()
except Exception as e:
print("ERROR:"+e.args[0])