Quantcast
Channel: Forums - Python
Viewing all 2485 articles
Browse latest View live

Weird File Path Returning from Print Statement Causing ERROR 000732

$
0
0
0 down vote favorite


I have created a python script in ArcGIS 10.1. My first parameter type is a 'Feature Layer' set to as a multivalue input. I was running my script quite well off of data from one of my drives (my Y:drive), but then I tried use some data from another drive (my Z:drive) and am receiving an error:

Quote:

ERROR 000732: Input Features: Dataset 'Z:\DATA FOR 2009\Base.gdb\CREEKS_UTM' does not exist or is not supported
I set a print message to print out the parameters for me and this is what i got:

Quote:

Y:\2012_data\INFRASTRUCTURE.gdb\Buildings;'Z:\DATA FOR 2009\Base.gdb\CREEKS_UTM';'Z:\DATA FOR 2009\Base.gdb\LAKES_UTM'
The only anomaly I see is that the 2 feature classes from the Z:drive is being wrapped in single quotes when it returns the value, but the Y:drive doesn't. I have no idea why this is, but I'm thinking that is the issue I am having.

My script basically just reads in the parameters and clips to the extent of a raster file I have. The reason for the conditional statement in the loop is because the user may use a layer file from ArcMap or they may navigate to a feature class/shapefile and use that as an input. If they are using the layer in the map, then it reads it in as just the layer name. If they navigate, then the parameter reads the whole file path and name, so I need to parse it to use the base file name for the output parameter in the clip tool.

Any suggestions are welcome.

My script is as follows:
Code:

import arcpy, os

inLines = arcpy.GetParameterAsText(0)
inRaster = arcpy.GetParameterAsText(1)
outRaster = arcpy.GetParameterAsText(2)

pnt_array = arcpy.Array()
extent = arcpy.Raster(inRaster).extent
pnt_array.add(extent.lowerLeft)
pnt_array.add(extent.lowerRight)
pnt_array.add(extent.upperRight)
pnt_array.add(extent.upperLeft)

poly = arcpy.Polygon(pnt_array)

for shp in inLines.split(';'):
    arcpy.AddMessage(shp)
    arcpy.AddMessage(os.path.isabs(shp))
    if os.path.isabs(shp) == True:
        arcpy.AddMessage('if')
        (filename, ext) = os.path.splitext(os.path.basename(shp))

        arcpy.Clip_analysis(shp, poly, outRaster + "\\" + str(filename) + "_Clip")       

    else:
        arcpy.AddMessage('else')
        arcpy.Clip_analysis(shp, poly, outRaster + "\\" + str(shp) + "_Clip")


Generic non hard coded select by attribute and location query - Help!

$
0
0
Hi,

I've written a script to import into Arc Tools (Toolbox) that finds suitable cities using a select by attribute and location query that is non hard coded and takes user input. All paths, shapefiles etc. are user input obtained when run as a tool in Arc Toolbox. In case it makes it clearer, the workspaces and shapefiles navigated to in GetParameterAsText are as follows-

workspace H:\working\FindSites.gdb
feature classes are "cities.shp", "counties.shp" and "interstates.shp"

I can't get the script to generate the output table, CityList.shp as required and can't seem to spot why not. I get the following message after it has ran-

Executing: EFLAgeneric H:\working\FindSites.gdb cities.shp 1 "CRIME_INDE" 1 "UNIVERSITY" counties.shp 2500 "AGE_18_64" 500 "NO_FARMS87" interstates.shp "30 Kilometers" CityList.shp
Start Time: Tue Apr 09 00:34:29 2013
Running script EFLAgeneric...
<type 'exceptions.NameError'>: name 'crimeField' is not defined
Failed to execute (EFLAgeneric).
Failed at Tue Apr 09 00:34:29 2013 (Elapsed Time: 0.00 seconds)

The crimeField (and others) are defined in the script and don't know why the error message says it's not defined.

Any help would be appreciated as I think I've run out of things to try.

Thanks

Scottaidh

[CODE]
# extract features by attrtibute generic non hard coded version
# scottaidh 9/4/13

# import system modules
import arcpy, os, traceback, arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0) # path is H:\working\Findsites.gdb


cities = arcpy.GetParameterAsText(1) # cities Layer is cities Feature Layer cities.shp
citiesL = "citiesL"
crimefieldindex = arcpy.GetParameterAsText(2) # crime index is CRIME_INDE and is a Double integer 0.02
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, crimeField), crimefieldindex)
crimeField = arcpy.GetParameterAsText(3) # crimeField is fieldname 'CRIME_INDE' SQL expression
universityfieldindex = arcpy.GetParameterAsText(4) # universityfieldindex is the UNIVERSITY field and is DOUBLE integer 1
whereClause2 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, universityField), universityfieldindex)
universityField = arcpy.GetParameterAsText(5) # universityField is fieldname 'UNIVERSITY' SQL expression
counties = arcpy.GetParameterAsText(6) # countiesL is counties Feature Layer counties.shp
countiesL = "countiesL"
workforceindex = arcpy.GetParameterAsText(7) # workforce index is attribute of AGE_18_64 field and is a Double and is 25000
whereClause3 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(countiesL, workforceField), workforcefieldindex)
workforceField = arcpy.GetParameterAsText(8) # workforceField is fieldname 'AGE_18_64' SQL expression
farmfieldindex = arcpy.GetParameterAsText(9) # farmfieldindex is the NO_FARMS87 field and is Double integer is 500
whereClause4 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(countiesL, farmField), farmfieldindex)
farmField = arcpy.GetParameterAsText(10) # farmField is fieldname 'NO_FARMS87' SQL expression
interstates = arcpy.GetParameterAsText(11) # interstatesL Layer is the interstates Feature Layer interstates.shp
# interstatesL = "interstatesL"
nearestInterstate = arcpy.GetParameterAsText(12) # interstate WITHIN_A_DISTANCE linear unit
# set output location
outputLayer = arcpy.GetParameterAsText(13) # output layer CityList.shp
targetFeatures = "cityListL"
joinFeatures = "countiesL"

# error trapping measures
try:
# make a layer from the cities feature class
arcpy.MakeFeatureLayer_management(cities, citiesL)

# select layer by location to interstates
arcpy.SelectLayerByLocation_management(citiesL, "WITHIN_A_DISTANCE", interstates, nearestInterstate, "NEW_SELECTION")

# from selection above select layer by attribute select "CRIME_IND" <= 0.02 AND "UNIVERSITY" = 1
arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", whereClause + " <= " + crimefieldindex + " AND " + whereClause2 + " = " + universityfieldindex)

# write selected features to a new featureclass
arcpy.CopyFeatures_management(citiesL, "SelectionCityLayer")

# make counties feature layer
arcpy.MakeFeatureLayer_management(counties,"countiesL")

# select workforce and number of farms
# new selection on counties layer countiesL "AGE_18_64" >= 25000 AND "NO_FARMS87" >= 500")
arcpy.SelectLayerByAttribute_management("countiesL", "NEW_SELECTION", whereClause3 + " >= " + workforceindex + " AND " + whereClause4 + " >= " + farmfieldindex)

# from selection above select cities intersecting counties
arcpy.SelectLayerByLocation_management("citiesL", "INTERSECT", "countiesL", "", "SUBSET_SELECTION")

# save selected features
arcpy.CopyFeatures_management("citiesL", "tempCityList")

# make temp cities list feature layer so that the output can be spatially joined to counties
arcpy.MakeFeatureLayer_management("tempCityList",targetFeatures)

# Generate NearTable_analysis to find closest interstate distance
arcpy.GenerateNearTable_analysis(targetFeatures, interstatesFC, NearestInterstate, nearestInterstate)

# join new city list layer to generated Near Table
arcpy.AddJoin_management(targetFeatures, "OBJECTID", NearestInterstate, "IN_FID")

#Run the Spatial Join tool, using the defaults for the join operation and join type
arcpy.SpatialJoin_analysis(targetFeatures, joinFeatures, outputLayer, "#", "#", "#")
# add field mappings later

except:
print arcpy.GetMessages()
[\CODE]

using python "In_Memory" for arcpy.conversion.ImportCADAnnotation

$
0
0
As the title states...

trying to gather thousands of dwgs annotation to one annotation featureclass in a FileGeodatabase.

the middle step, of using arcpy.conversion.ImportCadAnnotation failes:
Using "In_memory" would be using the Srcatch.gdb, but in catalog it does not require upgrading.

Runtime error Traceback (most recent call last): File "<string>", line 11, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\conversion.py", line 1687, in ImportCADAnnotation raise e ExecuteError: ERROR 000015: This Geodatabase version doesn't support the new annotation model. You should first upgrade it. Failed to execute (ImportCADAnnotation).

strange though, it works when pointing to it
r'C:\Users\michael.robb\Documents\ArcGIS\scratch.gdb'
... just not when using "In_memory".
?

Python gp.deletefeatures - PLEASE HELP

$
0
0
Hi Folks,
I have a script that extracts records from an online sharepoint list and appends them to a feature class... Every time the script runs the first thing it does is:

theFC = r"D:\GISScheduledTasks\LEDProjectExporter\...\GIS.LEDTrackingList"
gp.deletefeatures(theFC)

print "Features Deleted"

This is followed by a series of fieldList.append() statements for each column in the FC, which populates the whole thing right from Sharepoint.

Ideally I'd want to be able to edit features in the feature class, but currently doing so means any changes I make get overwritten the next time the script runs, since the FC gets deleted and re-created each time. I have a field in the FC that determines whether that row has been verified or not.. Verified = YES or NO. Id like to be able to update the FC from Sharepoint IF the Verified Column in the FC is NO, otherwise keep what is there..

Is there something I can use besides gp.deletefeatures or perhaps within an If statement block where this will work? any ideas?

Thanks
JS

Select by Location with user input

$
0
0
Hey everybody,

I have been fighting with this for a while. I was looking to create a button that starts a select by location script with the highlighted parts of select by attribute being configured by the user.

In example: The user clicks on the button and a popup box appears asking the user to select the layers he would like as target features (in_layer). These features are all found in the table of contents.
After selecting the layers another box pops up directing him to select the "select feature" layer (which there is a choice of 5 specific layers). These features are not in the table of content but in a geodatabase.


SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type})

Any help is appreciated!

How to assign variables to values inside fields.

$
0
0
Hello,

I want to assign variables to values inside fields.

There is one example of a script that retrieve values from a field and print them in the console. I need something like that but for get this values in different variables.

Thank you

Code:

import arcgisscripting
i = arcgisscripting.create(10.1)

rows= i.searchcursor("C:/test/testline.shp","","","FID,Id","")
row = rows.next()
currentstate = ""
while row:
    if currentstate != row.FID:
        currentstate = row.FID

        print (row.Id,row.FID)
    row = rows.next()

Server Tools, Extract Data Model, Email link instead of ZIP

$
0
0
I'm struggling with this one, and hope someone out there can help!

I have added a customization to the out of the box, ArcToolbox/Server Tool/Extract Data task, because if the data to download is over 1 Mb, I want the program to email a link to the user pointing to a folder location on the web server. This way the user can retrieve the ZIP containing the data via the web link, rather than emailing the ZIP itself.

Problem:
The model runs FINE in ArcMAP and emails the link correctly and puts the zip in the web folder:
http://my server.agency.net/gpt_download/fef8a5cf-3306-4b0e-99d2-56191bcdd124.zip

NOT FINE (Send Email With Zip File Attachment script):
When you use the Web GUI (and the geoprocessing service) it emails the link with the backslash directly before the UUID going the wrong direction.
http://myserver/gpt_download\7ae66941-153a-468d-92c1-a867f5f15dc9.zip
With the result being it can't put the zip in the folder with the backslash going the wrong direction.

Code:



import arcgisscripting, smtplib, os, sys, traceback
import shutil
import uuid

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

gp = arcgisscripting.create(9.3)
# CONFIGURE EMAIL MESSAGE AND EMAIL SERVER

# REMOVE BEFORE RELEASE
_email_server = "smtp.blm.gov"

_email_send_from = "BLM Geoportal"
_email_subject = "The ZIP file containing your area of interest"
_email_body_zip_file_attached = "Attached is a ZIP file containing the data in the area of interest you specified."
_email_body_zip_file_oversize = "The resulting ZIP file is too large (%sMB).  Must be less than 5MB.  Please digitize a smaller Area of Interest."

_email_body_link_attached = "Click the link below to open the ZIP file containing the area of interest you specified."

#**********************************************************************
# Description:
#  Emails a file. File is assumed to be a zip file. Routine either attaches
#  the zip file to the email or sends the URL to the zip file.
#
# Parameters:
#  1 - File to send.
#  2 - Email address to send file.
#  3 - Name of outgoing email server.
#  4 - Output boolean success flag.
#**********************************************************************

def send_mail(send_from, send_to, subject, text, f, server, smtpUser="", smtpPwd="", sendZip="" ):
    try:
        msg = MIMEMultipart()
        msg['From'] = send_from
        msg['To'] = COMMASPACE.join(send_to)
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = subject

        if not sendZip:        # send URL to zip file
            text = text + "\n\n" + f
            msg.attach ( MIMEText(text) )
        else:
            msg.attach( MIMEText(text) )
            part = MIMEBase('application', "zip")  # Change if different file type sent.
            part.set_payload( open(f,"rb").read() )
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
            msg.attach(part)
           
        smtp = smtplib.SMTP(server)
       
        # If your server requires user/password
        if smtpUser != "" and smtpPwd != "":
            smtp.login(smtpUser, smtpPwd)
       
        smtp.sendmail(send_from, send_to, msg.as_string())
        smtp.close()
    except:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n    " + \
                str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
        raise Exception("SendEmailError:" + pymsg)

 
if __name__ == '__main__':

    sendto = gp.GetParameterAsText(0).split(";")
    fromaddr = gp.GetParameterAsText(1)
    subject = gp.GetParameterAsText(2)
    text = gp.GetParameterAsText(3)
    zipfile = gp.GetParameterAsText(4).replace("\\",os.sep)
    maxsize = int(gp.GetParameterAsText(5)) * 1000000
    smtpMailServer = gp.GetParameterAsText(6)
    smtpUser = gp.GetParameterAsText(7)
    smtpPwd = gp.GetParameterAsText(8)
    sendZip = gp.GetParameterAsText(9)
   
    # If sendZip is true, the zip file will be attached to the email.
    # If false, the URL to the zip will be in the body of the email.
    #
    sendZip = False
    subject = _email_subject
   
    try:
        if sendZip:
            # Attach zip
            #
            text = _email_body_zip_file_attached
            #Don't email large zip files.
            #   
            zipsize = os.path.getsize(zipfile)
            #Message"Zip file size = "
            gp.AddMessage(gp.GetIDMessage(86156) + str(zipsize))
            if  zipsize <= maxsize:
                send_mail(fromaddr, sendto, subject, text, zipfile, smtpMailServer, smtpUser, smtpPwd, sendZip)
                #Message "Sent zipfile to %s from %s"
                gp.AddIDMessage("INFORMATIVE", 86154, sendto, fromaddr)
                gp.SetParameterAsText(9, "True")
            else:
                #Message "The resulting zip file is too large (%sMB).  Must be less than %MB.  Please
                # digitize a smaller Area of Interest."
                gp.AddIDMessage("ERROR", 86155, str(round(zipsize / 1000000.0, 2)),
                            str(round(maxsize / 1000000.0, 2)))
                gp.SetParameterAsText(9, "False")
                raise Exception

        else:
            # Send URL instead of .zip file.  The jobs and virtual directory can be found
            #  in service's properties dialog box.  UNIX note: this code converts pathnames
            #  to lower case to  do the string replace.
            #
            text = "Click the link below to open zip file containing the area of interest you specified."
            #--- Start custom change to Send URL ---#
            # Purpose: Copy zipfile to URL virtual directory
            # And give the zipfile a unique filename
            # Requires import shutil and import uuid
            #TP downloadDir = r"C:\\arcgisserverdata\gpt_download"
            downloadDir = r"\\myserver\arcgisserverdata\gpt_download"
            virtualDir = "http://myserver.agency.net/gpt_download"
            # First generate a filename with a random unique identifier using uuid.uuid4()
            zipDownload = os.path.join(downloadDir, str(uuid.uuid4()) + ".zip")
            # Next copy the Source zipfile to the new Destination zipDownload
            shutil.copy2(zipfile, zipDownload)
            # Finally replace the local file system path with the web-accessible directory URL path
            url = zipDownload.replace(downloadDir, virtualDir)
            # Replace backslashes with URL forward slash convention
            #url = url.replace("\\", "/")
            newurl = url.replace("\\","/")
           
            #send_mail(_email_send_from, [sendto], subject, text, newurl, _email_server, sendZip)   
            send_mail(fromaddr, sendto, subject, text, newurl, smtpMailServer, smtpUser, smtpPwd, sendZip)
    except:
        # Return any python specific errors as well as any errors from the geoprocessor
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n    " + \
                str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
        gp.AddError(pymsg)
        #Message "Unable to send email"
                #gp.AddIDMessage("ERROR", 86157)
        gp.AddError("ERROR, Unable to send email")
        #gp.AddError(gp.GetIDMessage(86157))

Zoom to ATS Location and Merge Features

$
0
0
Alright. Let me start off by saying I am completely new to python . I am a GIS student and for a project I have (ambitiously) decided to try and make a couple (I thought simple) script tools. The first one I want is to be able to have the user input which meridian, township, range, section, quarter section, and LSD they want to look at (yes just look at) and then have the map zoom to that area. I need the script to still work if any of the fields are left blank (they put in meridian, township, range but nothing else, for example). Simple right? I thought so but apparently my brain is too mushy to figure it out. I have no idea where to start.

The second thing i want to do is a little more tricky. I downloaded multiple NTS base features and need to (in the end) be able to rename them, define the projection, reproject them, merge like-features into one (all hydrology together, all roads together, etc), and then use this final feature in a cost distance analysis. What I've done for this is create a model that iterates through the folder I have all of my folders of base features in and rename them (Hydrology_0, Hydrology_1, etc) and defines the projection and then puts the shapefiles back into the folder of folders (so outside their original ones but inside the folder containing them). I then manually merged them and used them but is there any way to make this simpler using python?

Any help is greatly appreciated! Thanks so much

User Input Select By Attribute & Location - Desparate urgency - PLEASE HELP!!!!

$
0
0
I have written some generic code to run a select by attribute and location query and then do a spatial join with the narrowed down results. This is for a user input Arc Tool in Toolbox.

The trouble is that it doesn't output ANY of the output files (city_list.shp, foundcities.shp and NearTable.dbf). When I run the hard coded version of the script it outputs perfect results but when I get user input in the tool using GetParameterAsText it doesn't work.
Even a simple make feature layer and copy features command like below doesn't work when it's user input.
arcpy.MakeFeatureLayer_management(cities, citiesL)
arcpy.CopyFeatures_management("citiesL", cityListFC)

Please can you tell me what I've missed or where I'm going wrong. Why can't I get the output shapefile or dbf file?

I have a Friday deadline and I'm desparate and can't find anyone with an answer. Help would be highly appreciated and urgently needed.

Thanks Scott
Code:

# extract features by attrtibute generic non hard coded version
# scottaidh 9/4/13

# import system modules
import arcpy, os, arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0) # path is H:\working\Findsites.gdb
cities = arcpy.GetParameterAsText(1) # cities Layer is cities Feature Layer cities.shp
citiesL = "citiesL"
crimeField = arcpy.GetParameterAsText(2) # crimeField is fieldname 'CRIME_INDE' SQL expression
crimefieldindex = arcpy.GetParameterAsText(3) # crime index is CRIME_INDE and is a Double integer 0.02
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, crimeField), crimefieldindex)
universityField = arcpy.GetParameterAsText(4) # universityField is fieldname 'UNIVERSITY' SQL expression
universityfieldindex = arcpy.GetParameterAsText(5) # universityfieldindex is the UNIVERSITY field and is DOUBLE integer 1
whereClause2 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, universityField), universityfieldindex)
counties = arcpy.GetParameterAsText(6) # countiesL is counties Feature Layer counties.shp
countiesL = "countiesL"
workforceField = arcpy.GetParameterAsText(7) # workforceField is fieldname 'AGE_18_64' SQL expression
workforceindex = arcpy.GetParameterAsText(8) # workforce index is attribute of AGE_18_64 field and is a Double and is 25000
whereClause3 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(countiesL, workforceField), workforceindex)
farmField = arcpy.GetParameterAsText(9) # farmField is fieldname 'NO_FARMS87' SQL expression
farmfieldindex = arcpy.GetParameterAsText(10) # farmfieldindex is the NO_FARMS87 field and is Double integer is 500
whereClause4 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(countiesL, farmField), farmfieldindex)
interstates = arcpy.GetParameterAsText(11) # interstatesL Layer is the interstates Feature Layer interstates.shp
maxKmInterstate = arcpy.GetParameterAsText(12) # interstate WITHIN_A_DISTANCE linear unit
NearestInterstate = arcpy.GetParameterAsText(13) # Near Table Analysis table generated
cityListFC = arcpy.GetParameterAsText(14) # temp city list CityListFC
outputLayer = arcpy.GetParameterAsText(15) # output layer FoundCities.shp
targetFeatures = "cityListL" # cities spatial join layer
joinFeatures = "countiesL" # counties spatial join layer


# error trapping measures
try:
        # make a layer from the cities feature class
        arcpy.MakeFeatureLayer_management(cities, citiesL)
               
        # select layer by location to interstates
        arcpy.SelectLayerByLocation_management(citiesL, "WITHIN_A_DISTANCE", interstates, nearestInterstate, "NEW_SELECTION")
       
        # from selection above select layer by attribute select "CRIME_IND" <= 0.02 AND "UNIVERSITY" = 1
        arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", whereClause + " <= " + crimefieldindex + " AND " + whereClause2 + " = " + universityfieldindex)
               
        # make counties feature layer
        arcpy.MakeFeatureLayer_management(counties,countiesL)
       
        # select workforce and number of farms
        # new selection on counties layer countiesL  "AGE_18_64" >= 25000 AND "NO_FARMS87" >= 500")
        arcpy.SelectLayerByAttribute_management(countiesL, "NEW_SELECTION", whereClause3 + " >= " + workforceindex + " AND " + whereClause4 + " >= " + farmfieldindex)
               
        # from selection above select cities intersecting counties
        arcpy.SelectLayerByLocation_management(citiesL, "INTERSECT", countiesL, "", "SUBSET_SELECTION")
               
        # save selected features
        arcpy.CopyFeatures_management(citiesL, cityListFC)
       
        arcpy.AddField_management(cityListFC, "CITYNAME", "TEXT", "", "", "25")
        arcpy.AddField_management(cityListFC, "CRIMEINDEX", "DOUBLE", "", "", "")
        arcpy.AddField_management(cityListFC, "HAS_UNI", "TEXT", "", "", "3")
        arcpy.CalculateField_management(cityListFC, "CITYNAME", "!NAME!", "PYTHON")
        arcpy.CalculateField_management(cityListFC, "CRIMEINDEX", "!CRIME_INDE!", "PYTHON")
        arcpy.CalculateField_management(cityListFC, "HAS_UNI", "!UNIVERSITY!", "PYTHON")
        arcpy.DeleteField_management(cityListFC,["NAME", "LABEL", "CLASS"])
        arcpy.DeleteField_management(cityListFC,["CRIME_INDE", "LABEL", "CLASS"])
        arcpy.DeleteField_management(cityListFC,["UNIVERSITY", "LABEL", "CLASS"])
       
        # make temp cities list feature layer so that the output can be spatially joined to counties
        arcpy.MakeFeatureLayer_management(cityListFC,"cityListL")
       
        # Generate NearTable_analysis to find closest interstate distance
        arcpy.GenerateNearTable_analysis("cityListL", interstates, NearestInterstate, maxKmInterstate)
       
        # join new city list layer to generated Near Table
        arcpy.AddJoin_management("cityListL", "OBJECTID", NearestInterstate, "IN_FID")
       
        arcpy.CopyFeatures_management("cityListL", ouputLayer) # cityListFC)
       
        # Run the Spatial Join tool, using the defaults for the join operation and join type
        arcpy.SpatialJoin_analysis(targetFeatures, joinFeatures, outputLayer, "#", "#", "#")
        # add field mappings later
               
except:
        print arcpy.GetMessages()

Creating a python scrip to batch process .dat files

$
0
0
I have been trying to create a python scrip to batch process .dat files (with .hdr files).

The data is PRISM Temperature, Precip etc... data which are stored as .dat files - for all the type of functions I need, ArcGIS spatial analyst tools can do it if I do them 1-by-1. I have 3,100 files that need processing. This is what I need to happen

Mask the PRISM data to the study area.
Convert PRISM .dat to ASCII


I tried adapting the code I found below

Code:

# Extracts the cells of a raster that correspond with the areas
# defined by a mask.
# Author: Michel
# Date: 10 de Janeiro de 2011
# -----------------------------

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

try:
    # Check out any necessary licenses
    gp.CheckOutExtension("spatial")

    # Load required toolboxes...
    gp.AddToolbox("C:/Program Files (x86)/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

    gp.workspace = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PRISM_Binary/test"
    out_workspace = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PRISMPrecip"
    mask = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PolygonStudyArea/polygonstudyarea.shp"

    # Get a list of the rasters in the workspace
    raster = gp.ListRaster()

    # Loop through the list of rasters
    raster.reset()
    raster = raster.next()

    while raster:
        # Set the outputname for each output to be the same as the input
        output = out_workspace + raster

        # Process: Extract by Mask...
        gp.ExtractByMask_sa(raster, mask, output)

        # Loop function...
        raster = raster.next()

except:
    # If an error occurred while running a tool, then print the messages.
    print "Error in script"
    print gp.GetMessages()

But I've read that the tool in python has a known error that won't process .dat
The python code I cobbled clumsily together for mass converting to ASCII had no luck
Code:

# Description: Converts a raster datasets to an ASCII file representing
#    raster data from a foler. 
# Requirements: arcpy
# Author: Kenneth

# Import system modules
import arcpy
from arcpy import env

try:
# Set environment settings
    env.workspace = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PRISM_Binary/test"
    out_workspace = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PRISMPrecip"

# Set local variables
    raster = env.ListRaster()

#loop through raster list
    raster.reset()
    raster = raster.next()

    while raster:
        #set output name for each to be same as input
        outASCII = out_workspace + rasterlist
   
        # Execute RasterToASCII
        arcpy.RasterToASCII_conversion(raster, outASCII)

        #loop function
        raster = raster.next()
   
except:
    # If an error occurred while running a tool, then print the messages.
    print "Error in script"

Is there any other way I can mass change these files? I appreciate any help.

Same Code Different results HELP

$
0
0
Please can someone look at these two small pieces of code and tell me where I'm going wrong.
It's a simple(?) make feature layer and save feature layer command.

Code2 (below) works perfectly, but Code 1(below), doesn't and I don't know why. It seems I can make and save a feature layer when the script is had coded but not when it's user input.

When I run code1 I get the following error -

Executing: code1 H:\working\FindSites.gdb cities interstates counties H:\working\FindSites.gdb\OutList "30 Kilometers" "CRIME_INDE" 0.02 "UNIVERSITY" 1
Start Time: Wed Apr 10 13:20:47 2013
Running script code1...
<class 'arcgisscripting.ExecuteError'>: ERROR 999999: Error executing function.
General function failure
General function failure [interstates]
Failed to execute (SelectLayerByLocation).
Failed to execute (code1).
Failed at Wed Apr 10 13:20:49 2013 (Elapsed Time: 2.00 seconds)



Please look at it and tell me what I'm missing as it it is preventing me from progressing and I'm running out of time.

Thanks

Scottaidh

Code 2 - working perfectly
Code:

import os, sys, arcpy
arcpy.env.overwriteOutput = True
points = r"H:\working\Findsites.gdb\cities"
lines = r"H:\working\Findsites.gdb\interstates"
polys = r"H:\working\Findsites.gdb\counties"
outputFC = r"H:\working\Findsites.gdb\Outlist"
NearestInterstate = r"H:\working\Findsites.gdb\NearRoad"
           
arcpy.MakeFeatureLayer_management(points,"pointsL")
arcpy.SelectLayerByLocation_management("pointsL", "WITHIN_A_DISTANCE", lines, "30 KILOMETERS", "NEW_SELECTION")
arcpy.SelectLayerByAttribute_management("pointsL", "SUBSET_SELECTION", "UNIVERSITY = 1 AND CRIME_INDE <= 0.02")
arcpy.MakeFeatureLayer_management(polys,"polysL")
arcpy.SelectLayerByAttribute_management("polysL", "NEW_SELECTION", "AGE_18_64 >= 25000 AND NO_FARMS87 >= 500")
arcpy.SelectLayerByLocation_management("pointsL", "INTERSECT", "polysL", "", "SUBSET_SELECTION")
arcpy.CopyFeatures_management("pointsL", outputFC)
   
print arcpy.GetMessages()

Code 1 - generic version not working
Code:

# import system modules
import arcpy, os, arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0) # path is H:\working\Findsites.gdb
# arcpy.AddMessage(path + "ok")
points = arcpy.GetParameterAsText(1) # cities Layer is cities Feature Layer cities.shp
lines = arcpy.GetParameterAsText(2) # interstatesL Layer is the interstates Feature Layer interstates.shp
polys = arcpy.GetParameterAsText(3) # countiesL is counties Feature Layer counties.shp
outputLayer = arcpy.GetParameterAsText(4) # OutList.shp
distance = arcpy.GetParameterAsText(5) # min distance to road linear unit 30km
crimeField = arcpy.GetParameterAsText(6) # crimeField is fieldname 'CRIME_INDE' SQL expression
crimefieldindex = arcpy.GetParameterAsText(7) # crime index is CRIME_INDE and is a Double integer 0.02
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters("pointsL", crimeField), crimefieldindex)
universityField = arcpy.GetParameterAsText(8) # universityField is fieldname 'UNIVERSITY' SQL expression
universityfieldindex = arcpy.GetParameterAsText(9) # universityfieldindex is the UNIVERSITY field and is DOUBLE integer 1
whereClause2 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters("pointsL", universityField), universityfieldindex)

# error trapping measures

arcpy.MakeFeatureLayer_management(points, "pointsL")
# select layer by location to interstates
arcpy.SelectLayerByLocation_management("pointsL", "WITHIN_A_DISTANCE", lines, "30 KILOMETERS", "NEW_SELECTION")
       
# from selection above select layer by attribute select "CRIME_IND" <= 0.02 AND "UNIVERSITY" = 1
arcpy.SelectLayerByAttribute_management("pointsL", "SUBSET_SELECTION", whereClause + " <= " + crimefieldindex + " AND " + whereClause2 + " = " + universityfieldindex)
               
# make counties feature layer
arcpy.MakeFeatureLayer_management(polys,"polysL")
       
# from selection above select cities intersecting counties
arcpy.SelectLayerByLocation_management("pointsL", "INTERSECT", "polysL", "", "SUBSET_SELECTION")
               
# save selected features
arcpy.CopyFeatures_management("pointsL", outputLayer)
       
print arcpy.GetMessages()

Python code saved with extension .dll?

$
0
0
I have a python code that was saved with the extension .dll. How and what program would I open it with to see the program?

Calculate Field Tool Does not Complete - Help

$
0
0
I need help really bad. I have been looking through all of the Forum posts and and Help sections to try to figure out what is wrong with no luck. Here is what I am doing:
I have a File Geodatabase (ArcMap 10.0) with a Feature Class (Service Location) which is the physical location of our electric meters. I have multiple fields that I need to update on a nightly basis. This data comes from our CIS system which holds account, customer and meter attribute information. I have a script to runs automatically every night that pulls an Oracle VW through an ODBC connection and brings it into my File Geodatabase as a Geodatabase Table. What I have to do is to update a specific field (Account Number) in the Feature Class with the same field in the table (ACCOUNTNUM)... Just an example. What I want to be able to do is to also have this script run automatically at night. I have around 9500 records in the feature class with about 15 different fields to do this to. The problem I am having is it takes up to 20 to 25 minutes for each field to update so at times - the script can take 8 to 9 hours to complete. From what I understand - this should take only a few minutes.... So what I am doing is not working.
I decided to go back to the "Drawing Board" to see if there is something I am missing with writing the code..... I decided to work with (1) field right now to see if I can make it work and get the update time down before I rewrite the rest of the code.

This is what I currently have and it does not seem to be working:

Code:

## Set the necessary product code
import logging

# Import arcpy module
import arcpy
from arcpy import env


# if run in ArcGIS Desktop, show messages, also print to log
def log(method, msg):
    print msg
    method(msg)

logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s %(levelname)-s %(message)s',
        datefmt='%a, %d %b %Y %H:%M:%S',
        filename='Test_Update.log',
        filemode='w'
        )

log(logging.info, "Updating Service Locations Feature Class Begins")

# Set environment settings
env.workspace = "C:\\MEWCo GIS System\\Electric System\\MEWCo_Electric_Model-LOCAL.gdb"

# Set Local variables:
inFeatures = "SERVICE_LOCATION"                        # Service Location Feature Class
layerName = "SERVICE_LOCATION_LAYER"                    # Service Location Feature Layer
fieldName1 = "SERVICE_ADDRESS"
expression1 = "CIS_Account_Data.SERVICE_ADDRESS"
joinField = "POLE_NUMBER"
joinField2 = "MAPNO"
joinTable = "CIS_Account_Data"

# Create a Feature Layer from Service_Location Feature Class
log(logging.info, "Create Feature Layer for Service Location Feature Class")
arcpy.MakeFeatureLayer_management(inFeatures, layerName)

# Join the feature layer to a table
log(logging.info, "Join Table to Feature Layer Service Location")
arcpy.AddJoin_management(layerName, joinField, joinTable, joinField2)
   
# Process: Calculate Field (Member Number)
log(logging.info, "Update Member Number Field")
arcpy.CalculateField_management(layerName, "SERVICE_LOCATION.MEMBER_NUMBER", '!MEMBERNO!', "PYTHON")

log(logging.info, "Complete")

I am not sure what is slowing this down. Am I looking at this process the wrong way?? Are there different tools that can be used to complete this mission??

Thank you for your help

Scripting "Create Geometric Network" Syntax Error

$
0
0
Once again I have a syntax error that I cannot figure out. It keeps highlighting the end quotation marks which I have made RED.

Thanks for any input. This is my first attempt at scripting GeoNetworks so I could have other errors as well.

Code:

# Import arcpy module
import arcpy, sys, traceback
arcpy.env.overwriteOutput = True
from os import path as p

# Define Workspace directory
arcpy.env.workspace = top = r"G:\ChrisGIS\PS_Steelhead\Work"
#
# Create List of all Workspaces (GDB)
try:
    for ws in arcpy.ListWorkspaces("*", "FileGDB"):
        arcpy.env.workspace = ws
        print '\n\nSearching in %s\n\n' %ws
        #
        # Define Variables to be used in processes     
        BARRIERS = p.join(ws, "BARRIERS_MERGED_1")
        SHORE = p.join(ws, "NHD_EndVert_Shore_1")
        NHD = p.join(ws, "NHD_Split_Barriers_1")
        FDS = p.join(ws, "GEONET")
        #
        #
        # Process: Create Geometric Network
        arcpy.CreateGeometricNetwork(FDS, "NETGEO_Net", [BARRIERS "SIMPLE_JUNCTION" "NO", SHORE "SIMPLE_JUNCTION" "NO", NHD "SIMPLE EDGE"], "10", "","","","")
        print '\n\nGeometric Network created in  %s\n\n' %FDS

SelectLayerByAttribute using an 'in' argument

$
0
0
I'm trying to build a dynamic query that addresses user section of multiple values.

example: User input might be 1 or 1,2,3 or 1-5

I thought I could build a query such as

Code:

myfield = arcpy.AddFieldDelimiters(layer,"Number")

value = code to create an create an input list()
>>> value
[1, 2, 3, 4, 5, 6]

iQuery = '{0} in {1}'.format(value,myfield)

arcpy.SelectLayerByAttribute_management(layer,"NEW_SELECTION",iQuery)

Getting an 'Invalid expression Failed' error

Am I barking up the wrong tree with my methodology?

Thanks

Custom tool validation inside model builder

$
0
0
I have a custom tool built on a python script that has some code in initializeParameters in the ToolValidator. The code executes fine when I launch the tool, but if I add that custom tool to a model then the tool validation no longer seems to execute when I run the model. The code is population drop-down lists in the tool and I want those lists to be refreshable, but it looks like in order to make that happen I have to delete the tool from the model and re-add it; otherwise, the lists remain static.

The validation actually uses a python shelve to read in a list of values

in ToolValidation:

def initializeParameters(self):
import shelve
db = shelve.open(r"c:\mydata\lists")
list1 = db[0]
self.params[0].filter.list = list1
return

If I alter that validation code to try to change the list:

def initializeParameters(self):
self.params[0].filter.list = ["My", "New", "List"]
return

If I run the tool with this new validation code, it opens with the list updated correctly. But if I run the model that the tool was originally embedded in, it displays the old list--as if the validation code had not executed within the model. If I delete the tool from the model and re-add it, then it runs properly with the updated list. So it seems that ToolValidation does not run within a model for a custom script tool but instead the tools parameter values are static based on whatever values they had when the tool was originally added into the model.

Isolating features, who have no matching row in a table

$
0
0
I’ve been working with this for a while, but I feel like I must have hit a wall…
  • Everything is contained in one file geodatabase.
  • The point featureclass contains approximately 65000 features.
  • The table contains approximately 65000 rows.
  • The featureclass and the table have a key field that links them together.
My goal is to isolate those features, who have no matching row in the table and copy those features to a new featureclass.
To achieve this I’ve adopted the following approach:
  1. Use MakeFeatureLayer_management to create a layer from the point featureclass.
  2. Use AddJoin_management to join the table to the featureclass using the "KEEP_ALL" option
  3. Use MakeFeatureLayer_management to create another layer, with a where clause that selects only features where the joined key field is null.
  4. Open a search cursor on the feature layer from 3 and insert those features in a new featureclass.
This does NOT work. The feature layer defined in step 3 always contains no features.
To validate my approach, I tried doing the same thing in ArcMap. This did not work either.
To investigate further, I copied the featureclass and table to a personal geodatabase and tried again from ArcMap. 51 features were – as expected – selected.

How can this be done in a file geodatabase?
Is this the best way to isolate features, who have no matching row in the table and copy those features to a new featureclass?

Which sides of a parcel have road frontage.

$
0
0
Im trying to figure out how to find which 'side' of a land parcel has road frontage. The matter is complicated by our PA not storing true curves when they draw the parcels. Any cul-de-sac is made of hundreds of tiny lines each technically a side of the polygon. Ive converted polygons to lines and split at vertex but I cant figure out of the line fronts a road. I was thinking that if I can flag the segments as road front I could delete the other lines and merger the remaining back into longer lines by parcel ID. Any ideas on how to check this?

I need some help with os.walk and defining shapefiles in sub folders

$
0
0
So far this is what I have. It works if the shapefiles are in the C:/test_define folder, but it will fail with this error if they are in sub
folders. I am new to this so I am probably missing something simple, but I can't figure it out..

2000-10-Straight hole well.shp
--------------------------------------------------
Failed to execute. Parameters are not valid.
ERROR 000732: Input Dataset or Feature Class: Dataset 2000-10-Straight hole well.shp does not exist or is not supported
Failed to execute (DefineProjection).

My code
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import os
import arcpy

# Set workspace environment to a folder
WS = "c:/test_define"
arcpy.env.workspace = WS

for (path, dirs, files) in os.walk(WS):
for name in files:
if name.endswith(".shp"):
print name
print ("-") * 50
try:
infc = name
sr = arcpy.SpatialReference("NAD 1983 UTM Zone 11N")
arcpy.DefineProjection_management(infc, sr)

# print messages when the tool runs successfully
print(arcpy.GetMessages(0))

except arcpy.ExecuteError:
print(arcpy.GetMessages(2))

except Exception as ex:
print(ex.args[0])

Creating a Add-IN to select a feature in a feature class and displaying results.

$
0
0
I was wondering the solution would be to have button click, enable selection on a feature class (predefined) and reurning the attiributes. So you have turn select button on and off that is on arcmap. Can you do this or do you have the user already be in delect mode which defeats the purpose? I am new at python add ins so trying figure out syntax. This is just like the information button but I need to format the output.
Viewing all 2485 articles
Browse latest View live