分享最完美的蜂窝六边形的实现方法P

前言:使用Python完全实现蜂窝六边形要素类的创建,文章最后附工具和源码下载...什么是泰森多边形由于制作镶嵌六边的最后一步是创建泰森多边形,所以这里简单介绍一下什么是泰森多边形。泰森多边形多是用点图生成的。泰森多边形有以下几个性质:

每个泰森多边形内仅包含一个控制点。

且泰森多边形内的点与相应多边形内的控制点的距离最近。

边上的任一点到两边的控制点的距离一样。

所以使用距离位置合适的点生成的泰森多边形,就能生成蜂窝六边形。Python实现在《最完美的蜂窝六边形的实现方法-gpk实现》中,介绍使用打包好的.gpk工具实现制作蜂窝六边形要素类。在该章节中,我们将完全使用Python来实现这一功能。实现原理:

首先获取输入的矢量数据的坐标位置等信息info1;

使用坐标位置信息info1计算出合适的偏移坐标位置信息info2;

使用info1和info2各自生成渔网,包括其中的点;

合并点,生成泰森多边形;

按位置选择,导出结果。

直接上代码(HexagonCreator.py)及其注释说明:

#-*-coding:utf-8-*-#---------------------------------------------------------------------------#Author:LiaoChenchen#Createdon:/4/:00#Reference:"""Description:Usage:"""#---------------------------------------------------------------------------from__future__importabsolute_importfrom__future__importunicode_literalsimportosimportarcpyimportmathclassHexPolygon(object):def__init__(self,in_f,out_f,width=):"""创建镶嵌六边形:paramin_f:输入要素:paramout_f:输出要素:paramwidth:六边形的宽,注意不是边长"""self.in_f=in_fself.out_f=out_fself.width=float(width)descinput=arcpy.Describe(self.in_f)self.ref=arcpy.Describe(self.in_f).spatialReference#___Function___result=self.cal_extent()self.create_flow(*result)defcal_extent(self):"""计算出两幅渔网的坐标信息:return:返回两幅渔网矢量的信息"""desc=arcpy.Describe(self.in_f)ext=desc.extentx_min=ext.XMinx_max=ext.XMaxy_min=ext.YMiny_max=ext.YMax#calculateoffsetvalueself.height=self.width*math.sqrt(3)self.width,self.height=self.height,self.width#Calculatenewoffsetorigin,oppositecornerand#Yaxispointcoordinatesfactor1=-2.0#?注释1?origin_x=x_min+self.width*factor1origin_y=y_min+self.height*factor1origin=str(origin_x)+""+str(origin_y)#Theoppositecornerofthefishnetsetfactor2=2.0corner_coordx=x_max+self.width*factor2corner_coordy=y_max+self.height*factor2corner_coord=str(corner_coordx)+""+str(corner_coordy)#新原点factor3=0.5new_origin_x=str(origin_x+self.width*factor3)new_origin_y=str(origin_y+self.height*factor3)new_origin=new_origin_x+""+new_origin_y#newoppositecornercorner_coordx2=str(corner_coordx+self.width*factor3)corner_coordy2=str(corner_coordy+self.height*factor3)corner_coord2=corner_coordx2+""+corner_coordy2#note:使用的是stry_coord1=str(origin_x)+""+str(corner_coordy)y_coord2=new_origin_x+""+corner_coordy2#CalculateLength,hexagonalareaandnumberofcolumnshexg_len=float(self.height)/math.sqrt(3)#等边六边形面积计算公式:根号3*3/2*边长*边长hexg_area=math.sqrt(3)*3/2*pow(hexg_len,2)arcpy.AddMessage("OneHexagonCellArea:"+str(hexg_area))vector1=(origin,y_coord1,corner_coord)vector2=(new_origin,y_coord2,corner_coord2)returnvector1,vector2defcreate_flow(self,vector1,vector2):"""开始创建六边形:paramvector1:用于创建渔网的坐标数据:paramvector2:另一个用于创建渔网的坐标数据:return:"""workspace=os.path.dirname(self.out_f)arcpy.env.scratchWorkspace=workspace#arcpy.env.overwriteOutput=True#------firstfishnet------#fishnet1_point-fishnet1_ppoint#fishnet1_result-fishnet1_res#fishnet1_label-fishnet1_lbCF=arcpy.CreateFishnet_managementfishnet1_path=(os.path.join(workspace,"Fishnet1"))fishnet1=CF(fishnet1_path,vector1[0],vector1[1],self.width,self.height,"0","0",vector1[2],"LABELS")#------secondfishnet------fishnet2_path=(os.path.join(workspace,"Fishnet2"))fishnet2=CF(fishnet2_path,vector2[0],vector2[1],self.width,self.height,"0","0",vector2[2],"LABELS")#labelpointfishnet1_lb=fishnet1.getOutput(1)fishnet2_lb=fishnet2.getOutput(1)arcpy.DefineProjection_management(fishnet1_lb,self.ref)arcpy.DefineProjection_management(fishnet2_lb,self.ref)#将新旧标注点(label)合并#?注释2?full_pt=arcpy.Append_management(fishnet2_lb,fishnet1_lb)#CreateThiessenPolygons#?注释3?full_theissen=arcpy.CreateThiessenPolygons_analysis(full_pt,(os.path.join(workspace,"FullTheissen")))#1.将完整的泰森多边形创建为要素图层#2.按位置选择出和输入目标图层相交的部分#3.导出要素图层f_lyr="_lyr"arcpy.MakeFeatureLayer_management(full_theissen,f_lyr)arcpy.SelectLayerByLocation_management(f_lyr,"INTERSECT",self.in_f)#?注释4?arcpy.CopyFeatures_management(f_lyr,self.out_f)#Deleteintermediatedataarcpy.Delete_management(fishnet1)arcpy.Delete_management(fishnet2)arcpy.Delete_management(fishnet1_lb)arcpy.Delete_management(fishnet2_lb)arcpy.Delete_management(full_theissen)arcpy.Delete_management(f_lyr)arcpy.AddMessage("Completedhexagonalpolygons.")整个工具封装成了一个类。类中共有三个方法包括初始化方法。cal_extent方法:主要是计算出两套渔网坐标。每一套坐标包含一个起始原点、y轴坐标、渔网的对角坐标。渔网坐标是用于生成渔网的,参数详情可以看:



转载请注明地址:http://www.xqopn.com//zcmbwh/92844.html
  • 上一篇文章:
  • 下一篇文章: