More Related Content
Similar to CellCoverage (20)
CellCoverage
- 1. # CellCoverage.py
# CreatedbyBPfeiffer3/7/2016
# Programtakesinputs
# 1) a boundaryshapefileforthe region(eg,county,state,region)
# 2) a DEM of the area that mustcover a 24 kilometerbufferof the boundary
# 3) a .csv file of Cell towerlocationswithLattitude,Longitude,andheightof the tower
# 3) a textfile containingastreetname,Lattitude,andLongitudeof watermeters
# 4) thisprogram takesa localRoads.shp,majorRoads.shp,andhighways.shpforreferenceforthe final
map
# Thisprogram calculatesCell receptionbasedontowerlocationsandviewshedanalysis. The outputis
4 gradesof cell
# zonesbasedonviewshed,proximity,andoverlap. Italsooutputs4 shapefilesof watermetersbased
on theircell
# receptionrating. Finally,the programwritesatextsplittingall watermetersintotheir4categories.
importarcpy
fromarcpy.sa import*
arcpy.env.overwriteOutput=True
############# Set up
Function################################################################
def roadsCheck(roadFile,tempfile,outfile,roadName,SpatialReference,boundary):
arcpy.env.overwriteOutput=True
desc = arcpy.Describe(roadFile)
if desc.shapeType =="Polyline":
arcpy.Project_management(roadFile,temp+roadName,spatRef)
arcpy.Clip_analysis(temp+roadName +".shp",gdbFullPath+""+ "boundary",gdbFullPath+""
+ roadName)
else:
print"The shapefileisnota Polyline. Checkyourfile"
#####################################################################################
########
- 2. # Setup inputshapefiles.txtfileand.csvfile
CellTowers="C:Usersbrian_000DesktopGIS4080PythonFinal ProjectInFileCellTowers.csv"
DEM = "C:Usersbrian_000DesktopGIS4080PythonFinal ProjectInFileCO_DEM.lyr"
boundary= "C:Usersbrian_000DesktopGIS4080PythonFinal
ProjectInFileBoulderCounty.lyr"
WaterMeters= "C:Usersbrian_000DesktopGIS4080PythonFinal
ProjectInFileWaterMeters.txt"
localRoads= "C:Usersbrian_000DesktopGIS4080 PythonFinal
ProjectInFileLOCAL_ROADS.shp"
majorRoads= "C:Usersbrian_000DesktopGIS4080PythonFinal
ProjectInFileMAJOR_ROADS.shp"
highways= "C:Usersbrian_000DesktopGIS4080PythonFinalProjectInFileHIGHWAYS.shp"
# Setup Folderpaths
temp= "C:Temp"
outFile ="C:Usersbrian_000DesktopGIS4080PythonFinal ProjectOutFile"
# Setup all variables
spatRef = arcpy.SpatialReference(4269)
heightList=[]
streetList=[]
streetList2= []
visibleList=[]
problemList=[]
CTCount= 0
w = 0
x = 0
y = 1
z = 3
p = 0
- 3. # Create Geodatatabase andsetpaths
gdbName = "Cell.gdb"
gdbFullPath=outFile +gdbName
try:
if not arcpy.Exists(gdbFullPath):
arcpy.CreateFileGDB_management(outFile,gdbName)
print"Geodatabase Ready"
except:
print"Failedtocreate Geodatabase"
# Error Checkboundaryfile,reproject,andimportintoGeodatabase
desc= arcpy.Describe(boundary)
if desc.shapeType=="Polygon":
arcpy.Project_management(boundary,gdbFullPath+""+ "boundary",spatRef)
desc = arcpy.Describe(gdbFullPath+""+ "boundary")
extentBoundary=desc.Extent
print"boundaryhas beensucessfullyreprojectedandimportedtoGeodatabase"
arcpy.Buffer_analysis(gdbFullPath+""+ "boundary",temp+ "AOI","24 kilometers","FULL")
desc = arcpy.Describe(temp+"AOI"+ ".shp")
extentAOI=desc.Extent
print"AOI hasbeencreated"
else:
print"Boundaryshapefile isnotPolygon"
# Error CheckDEM file,cliptoextentAOI,reproject,andimportintoGeodatabase
desc= arcpy.Describe(DEM)
if desc.datasetType =="RasterDataset":
arcpy.ProjectRaster_management(DEM,temp+ "fullDEM",spatRef)
arcpy.CheckOutExtension("Spatial")
rectExtract = ExtractByRectangle(temp+"fullDEM",extentAOI,"INSIDE")
- 4. rectExtract.save(gdbFullPath+""+ "DEM")
print"DEM has beensucessfullyreprojected,clipped,andimportedintoGeodatabase"
arcpy.CheckOutExtension("Spatial")
else:
print"DEM is nota RasterDataset"
# Error CheckCell Towers.csvfile,create Cell Towershapefile
desc= arcpy.Describe(CellTowers)
if desc.DataType == "TextFile":
for fieldindesc.fields:
if field.name =="STREET":
print"STREET fieldexistsinCellTowertextfile"
elif field.name=="LAT":
print"LAT fieldexistsinCellTowertextfile"
elif field.name=="LONG":
print"LONG fieldexistsinCellTowertextfile"
elif field.name=="HEIGHT":
print"HEIGHT fieldexistsinCellTowertextfile"
else:
printfield.name +" fieldinCell Towertextfile isnotneededorincorrect. Tryreformatting.csv
file if problemarises"
try:
arcpy.CreateFeatureclass_management(temp,"CTtemp","POINT","","","", spatRef)
arcpy.AddField_management(temp+"CTtemp.shp","NAME","TEXT")
arcpy.AddField_management(temp+"CTtemp.shp","HEIGHT","DOUBLE")
print"Cell Towers shapefile hasbeencreatedandfieldshave beenadded"
except:
print"Failedtocreate Cell Towersshapefile"
- 5. # Write geometryforCell Towers.csv,clipCell TowerstoAOI,reproject,anduploadshapefile to
Geodatabase
try:
cursor= arcpy.da.SearchCursor(CellTowers,["STREET","LONG","LAT","HEIGHT"])
cursor2 = arcpy.da.InsertCursor(temp+"CTtemp.shp",["SHAPE@"])
forrow incursor:
streetList.append(row[0])
heightList.append(row[3])
pointlist=arcpy.Array()
point= arcpy.Point(row[1],row[2])
pointlist.add(point)
cursor2.insertRow(pointlist)
del cursor
del cursor2
print"SucessfulywortegeometryforCell Towers"
except:
print"Failedtowrite geometry"
try:
cursor= arcpy.da.UpdateCursor(temp+"CTtemp.shp",["NAME","HEIGHT"])
forrow incursor:
row[0] = streetList[x]
row[1] = heightList[x]
cursor.updateRow(row)
x = x + 1
print"Cell TowerShapefilehasbeenupdated"
except:
print"Failedtoupdate Cell Towershapefile"
del cursor
try:
- 6. arcpy.MakeFeatureLayer_management(temp+"CTtemp.shp","CTtempLyr")
arcpy.SelectLayerByLocation_management("CTtempLyr","WITHIN",temp+"AOI.shp","",
"NEW_SELECTION")
CTCount= int(arcpy.GetCount_management("CTtempLyr").getOutput(0))
except:
print"FailedtoclipCell Towerpointstoboundary"
if CTCount > 0:
try:
arcpy.Project_management("CTtempLyr",gdbFullPath+""+ "CT" , spatRef)
print"Projectionhassucceededand"+ str(Count) + " Cell Towerlacationshave beenuploadedto
Geodatabase"
except:
print"FailedtoreprojectCell Towershapefile"
else:
print"Cell Tower.csvis nota textfile orismissingdata. Checkfile forerrors"
# errorcheck WaterMeters .txt,write geometry,andupdate name field
desc= arcpy.Describe(WaterMeters)
if desc.DataType == "TextFile":
try:
arcpy.CreateFeatureclass_management(temp,"WMtemp","POINT","","", "",spatRef)
arcpy.AddField_management(temp+"WMtemp.shp","NAME","TEXT")
print"WaterMeters shapefile hasbeencreatedandfieldshave beenadded"
except:
print"Failedtocreate WaterMeters shapefile"
try:
txt= open(WaterMeters)
cursor= arcpy.da.InsertCursor(temp+"WMtemp.shp","SHAPE@")
forline intxt:
lineseg=line.split(",")
- 7. if str(lineseg[1][0]) =="-"and len(lineseg[2]) >12 and lineseg[0] !="":
streetList2.append(lineseg[0])
pointlist2=arcpy.Array()
point= arcpy.Point(lineseg[1], lineseg[2])
pointlist2.add(point)
cursor.insertRow(pointlist2)
else:
problem= lineseg[0] +", " + lineseg[1] +"," + lineseg[2]
problemList.append(problem)
del cursor
txt.close()
if len(problemList) >0:
print"!!!THE FOLLOWINGLINES CONTAIN ERRORS!!!"
printproblemList
else:
print"Noerrors detectedinWaterMeterlist"
except:
print"Failedtowrite geometryforWaterMeters"
try:
cursor= arcpy.da.UpdateCursor(temp+"WMtemp.shp",["NAME"])
forrow incursor:
row[0] = streetList2[w]
cursor.updateRow(row)
w = w + 1
del cursor
print"WaterMeter shapefile hasbeenupdated"
except:
print"Failedtoupdate WaterMetershapefile"
#Checkthat Water Meterpointsare withinAOIandloadshapefile intoGeodatabase
- 8. try:
arcpy.MakeFeatureLayer_management(temp+"WMtemp.shp","WMLyr")
arcpy.SelectLayerByLocation_management("WMLyr","WITHIN",gdbFullPath+""+ "boundary",
"", "NEW_SELECTION")
Count= int(arcpy.GetCount_management("WMLyr").getOutput(0))
except:
print"FailedtoclipWaterMeter pointstoboundary"
if Count > 0:
try:
arcpy.Project_management("WMLyr",gdbFullPath+""+ "WM" , spatRef)
print"Projectionhassucceededand"+ str(Count) + " Water Meterlacationshave beenuploaded
to Geodatabase"
except:
print"FailedtoreprojectWaterMetershapefile"
else:
print"Water Meter .txtisnot a textfile orismissingdata. Checkfile forerrors"
# use functiontoerror checkroads,reproject,clip toboundary,anduploadto Geodatabase
try:
roadsCheck(localRoads,temp,gdbFullPath+"","localRoads",spatRef,gdbFullPath+""+
"boundary")
roadsCheck(majorRoads,temp,gdbFullPath+"","majorRoads",spatRef,gdbFullPath+""+
"boundary")
roadsCheck(highways,temp,gdbFullPath+"","highways",spatRef,gdbFullPath+""+
"boundary")
print"Roads and Highwayshave beenreprojectedandimportedtoGeodatabase"
except:
print"FailedtouploadRoadsand Highways"
CTName = "CT"
- 9. CT = gdbFullPath+""+ CTName
inRast= gdbFullPath+""+ "DEM"
# Setvariablesforviewanalysis
CTLyr = temp+ CTName
Ring= CTLyr + "Ring"
Polygon= temp+ CTName + "Vispoly"
PolyBuff =Polygon+ "Buff"
Visible=temp+ "Visible"
# CheckoutSpatial extension
try:
if arcpy.CheckExtension("Spatial") =="Available":
arcpy.CheckOutExtension("Spatial")
print"Spatial extensioncheckedout"
except:
raise LicenseError
# SelecteachCell Tower,create view analysis,create polygon,bufferpolygon,cliptoringsbuffer,
update value field
while True:
try:
arcpy.MakeFeatureLayer_management(CT,"CTLyr")
arcpy.SelectLayerByAttribute_management("CTLyr","NEW_SELECTION",'"OBJECTID"=' + str(y))
arcpy.MultipleRingBuffer_analysis("CTLyr",Ring+str(y),[8,16, 24], "kilometers","","ALL")
Vis= arcpy.sa.Visibility(inRast,"CTLyr","","","NODATA","","", "", "","", "HEIGHT")
Vis.save(Visible+str(y))
arcpy.RasterToPolygon_conversion(Visible +str(y),Polygon+str(y),"SIMPLIFY","VALUE")
poly= CTName.lower()
arcpy.MakeFeatureLayer_management(temp+poly+ "vispoly"+str(y) + ".shp",Polygon+str(y) +
"lyr")
arcpy.SelectLayerByAttribute_management(Polygon+str(y) + "lyr","NEW_SELECTION",
'"GRIDCODE" = 1')
- 10. arcpy.Buffer_analysis(Polygon+str(y) + "lyr",PolyBuff +str(y),"200 meters","","","ALL")
arcpy.Clip_analysis(Ring+str(y) +".shp",PolyBuff+str(y) + ".shp",Visible +str(y))
visibleList.append(Visible +str(y) + ".shp")
arcpy.AddField_management(Visible +str(y) + ".shp","VALUE","SHORT")
cursor= arcpy.da.UpdateCursor( Visible +str(y) +".shp",["VALUE"])
visibleList.append(Visible +str(y) + ".shp")
forrow incursor:
row[0] = z
cursor.updateRow(row)
z = z -1
print"Visible"+str(y) + " has beensucessfullycreated"
y= y + 1
del cursor
except:
print"Failedtocreate all visiblelayers"
else:
print"Error withData. Cell Towersmaynotfall withinAOI"
arcpy.CheckInExtension("Spatial")
# Setup variablesforunionandfinal shapefiles
BR = "Bad_Reception"
GoodR = "Good_Reception"
VGR = "VeryGood_Reception"
GR = "Great_Reception"
BRLyr = "BRLyr"
UnionLyr= "UnionLyr"
union= temp+ "UnionAll"
try:
arcpy.Union_analysis(visibleList,union)
- 11. arcpy.Clip_analysis(union+".shp",gdbFullPath+""+ "boundary",union+"_clip.shp")
arcpy.MakeFeatureLayer_management(union+"_clip.shp",UnionLyr)
arcpy.Erase_analysis(gdbFullPath+"" + "boundary",union+"_clip.shp",gdbFullPath+""+ BR)
arcpy.MakeFeatureLayer_management(temp+"Bad_Reception.shp",BRLyr)
arcpy.SelectLayerByAttribute_management("UnionLyr","NEW_SELECTION",'"VALUE"= 0')
arcpy.Dissolve_management("UnionLyr",gdbFullPath+""+ GoodR)
arcpy.SelectLayerByAttribute_management("UnionLyr","NEW_SELECTION",'"VALUE"= 1')
arcpy.Dissolve_management("UnionLyr",gdbFullPath+""+ VGR)
arcpy.SelectLayerByAttribute_management("UnionLyr","NEW_SELECTION",'"VALUE"> 1')
arcpy.Dissolve_management("UnionLyr",gdbFullPath+""+ GR)
print"Union shapefile hasbeencreatedanduploaded intoGeodatabase"
except:
print"Failedtocreate Union shapefile"
# Write an outputfile "WaterMeterList.txt"thatlistseachwatermeterbycategory
try:
WMtxt = open(outFile+"WaterMeterList.txt","w")
WMtxt.write("WaterMeterListbyVerizonCell ReceptionnnBadorNoCell Receptionn")
cursor = arcpy.da.SearchCursor(gdbFullPath+""+ "WM_" + BR, ["NAME"])
for row incursor:
WMtxt.write(row[0] +"n")
del cursor
WMtxt.write("nGoodCell Receptionn")
cursor = arcpy.da.SearchCursor(gdbFullPath+""+ "WM_" + GoodR,["NAME"])
for row incursor:
WMtxt.write(row[0] +"n")
del cursor
WMtxt.write("nVeryGoodCell Receptionn")
cursor = arcpy.da.SearchCursor(gdbFullPath+""+ "WM_" + VGR,["NAME"])
for row incursor:
- 12. WMtxt.write(row[0] +"n")
del cursor
WMtxt.write("nGreatCell Receptionn")
cursor = arcpy.da.SearchCursor(gdbFullPath+""+ "WM_" + GR, ["NAME"])
for row incursor:
WMtxt.write(row[0] +"n")
del cursor
WMtxt.close()
print"WaterMeterList.txtwassucessfullycreatedandwritten"
except:
print"Failedtocreate WaterMeterList.txt"
print"!!!!!!!!!!!!!Programhasfinishedrunning!!!!!!!!!!!!!!!!!!!!!"