arcpy开发实例之管线自动构建
# _*_ encoding: utf-8 _*_
import arcpy
arcpy.env.workspace = "d:/temp/pipe_data";
if arcpy.Exists("points.shp"):
arcpy.management.Delete("points.shp");
arcpy.management.CreateFeatureclass("d:/temp/pipe_data","points.shp","POINT",spatial_reference=arcpy.SpatialReference(3857));
if arcpy.Exists("lines.shp"):
arcpy.management.Delete("lines.shp");
arcpy.management.CreateFeatureclass("d:/temp/pipe_data","lines.shp","POLYLINE",spatial_reference=arcpy.SpatialReference(3857));
arcpy.AddField_management("points","expno","TEXT",5);
arcpy.AddField_management("points","mapno","TEXT",5);
arcpy.AddField_management("points","surfh","FLOAT");
arcpy.AddField_management("points","wdeep","FLOAT");
arcpy.AddField_management("lines","spoint","TEXT",5);
arcpy.AddField_management("lines","epoint","TEXT",5);
# 读取XLS
import xlrd
xls = xlrd.open_workbook("d:/temp/pipe_data/ws_point.xlsx");
sht = xls.sheets()[0];
rn = sht.nrows;
cn = sht.ncols;
points = [];
with arcpy.da.InsertCursor("points",["expno","mapno","surfh","wdeep","SHAPE@XY"]) as cursor:
for i in range(1,rn):
print " Read row : ",i;
ctype = sht.cell_type(i,0);
if ctype == 0:
expno="";
elif ctype ==1 :
expno = sht.cell(i,0).value;
ctype = sht.cell_type(i,1);
if ctype == 0:
mapno="";
elif ctype ==1 :
mapno = sht.cell(i,1).value;
ctype = sht.cell_type(i,4);
if ctype == 0:
surfh=0;
elif ctype ==1 :
surfh = float(sht.cell(i,4).value);
elif ctype==2:
surfh = sht.cell(i,4).value;
else:
surfh = 0;
ctype = sht.cell_type(i,5);
if ctype == 0:
wdeep=0;
elif ctype ==1 :
wdeep = float(sht.cell(i,5).value);
elif ctype==2:
wdeep = sht.cell(i,5).value;
else:
wdeep = 0;
ctype = sht.cell_type(i,2);
if ctype == 0 or ctype>2:
continue;
elif ctype == 1:
x = float(sht.cell(i,2).value);
else:
x = sht.cell(i,2).value;
ctype = sht.cell_type(i,3);
if ctype == 0 or ctype>2:
continue;
elif ctype == 1:
y = float(sht.cell(i,3).value);
else:
y = sht.cell(i,3).value;
row = [expno,mapno,surfh,wdeep,(x,y)];
points.append(row);
cursor.insertRow(row);
# 读取线表,并构建线
xls = xlrd.open_workbook("d:/temp/pipe_data/ws_line.xlsx");
sht = xls.sheets()[0];
rn = sht.nrows;
cn = sht.ncols;
with arcpy.da.InsertCursor("lines",["spoint","epoint","SHAPE@"]) as cursor:
for i in range(1,rn):
print " Read row : ",i;
ctype = sht.cell_type(i,1);
if ctype !=1:
continue;
else :
spoint = sht.cell(i,1).value;
ctype = sht.cell_type(i,2);
if ctype != 1:
continue;
else :
epoint = sht.cell(i,2).value;
for x in points:
if x[0]==spoint:
pt1 = x;
break;
for x in points:
if x[0]==epoint:
pt2 = x;
break;
arr = arcpy.Array();
arr.append(arcpy.Point(pt1[4][0],pt1[4][1]));
arr.append(arcpy.Point(pt2[4][0],pt2[4][1]));
line = arcpy.Polyline(arr);
row = [pt1[0],pt2[0],line];
cursor.insertRow(row);

