0

I am a bit stuck with my code right now. I have a two feature classes, one for schools, one for residential buildings. So far I have built a model with the modelbuilder, whichs works perfectly as well as the exported python code. The aim is to create service areas around the school and then calculate the distance from the building, which are located in the service area to the school. My problem now is, that I want to do this one by one. At the moment it calculates all the service areas at once. I know I can access single features with the arcpy.da.SearchCursor but then I am stuck.

So far I got this code for my problem:

searchField = "FID"
with arcpy.da.SearchCursor(schoolPoints,searchField) as cursor:
for row in cursor:

    singleSchoolCounter += 1
    where = '"FID" = ' +"%s" %singleSchoolCounter
    #arcpy.AddMessage("Shapefile %s has %s rows " %(schoolPoints, singleSchoolCounter))
    for schools in range (0, singleSchoolCounter):
        ServiceAreaSchool = "ServiceArea" +"%s" %singleSchoolCounter
        arcpy.Select_analysis(schoolPoints,oneSchool,where)
        arcpy.AddMessage("Start calculating Service Area for School #%s" %(singleSchoolCounter))

        #Calculate the service Areas for the schools
        #create a new Service Area Layer
        arcpy.na.MakeServiceAreaLayer(networkDataset, "ServiceArea"+"%s" %singleSchoolCounter, "Meters", "TRAVEL_TO", "800, 1000", "SIMPLE_POLYS", "NO_MERGE", "RINGS", "NO_LINES", "OVERLAP", "NO_SPLIT", "", "", "ALLOW_UTURNS")
        #Add the schools
        arcpy.na.AddLocations(ServiceAreaSchool, "Facilities", oneSchool, "", "")
        #solve the Service area layer
        arcpy.na.Solve(ServiceAreaSchool)
        serviceAreaCounter += 1
        arcpy.SaveToLayerFile_management("serviceArea" +"%s" %singleSchoolCounter,outAreaFile,"RELATIVE") 
        #Save solved Area to Disk
        arcpy.AddMessage("%s successfully built" %(ServiceAreaSchool))

It works until solving. There I get this error:

ExecuteError: ERROR 030024: Solve returned a failure. Invalid argument. Failed to execute (Solve).

Maybe someone can help?

  • 1
    Show what you´ve got so far. – HimBromBeere Apr 13 '16 at 11:01
  • I added my code – Aldi Kasse 2 Apr 13 '16 at 18:36
2

Based on the example provided here: http://pro.arcgis.com/en/pro-app/tool-reference/network-analyst/make-service-area-layer.htm

Some things you might consider changing:

ServiceAreaSchool = "ServiceArea%s" % singleSchoolCounter
result_object = arcpy.na.MakeServiceAreaLayer(networkDataset, ServiceAreaSchool, "Meters", "TRAVEL_TO", "800, 1000", "SIMPLE_POLYS", "NO_MERGE", "RINGS", "NO_LINES", "OVERLAP", "NO_SPLIT", "", "", "ALLOW_UTURNS")
layer_object = result_object.getOutput(0)
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
facilities_layer_name = sublayer_names["Facilities"]
schoolLayer = arcpy.MakeFeatureLayer_management(schoolPoints, "School%s"%(singleSchoolCounter),where)[0]
arcpy.na.AddLocations(layer_object, facilities_layer_name, schoolLayer, outAreaFile,"RELATIVE")
arcpy.na.Solve(layer_object)

A couple of other notes:

Try this just once before looping to get it working for at least one feature. You will probably want to delete the layer once you are looping to restart, or create the serviceAreaLayer outside of your loop, and then clear the facilities every time the loop runs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.