POST
|
I have a feature service created using ArcGIS for Server and using python i want to select feature(s) based on OBJECTID and delete them. The user will be selecting the features to delete in Arcmap. I know i am able to create a local copy, delete them manualy and sync back to the service but i want to do all the lifting via python. Any help would be appreciated. Below is what i have so far the section "Choose orginal features to delete" is what i can not get working, the rest of the code works fine. import arcpy import os from arcpy import env env.overwriteOutput = True # Variables... in_datavarPOI = r'U:\Tools\SDE_Connection_Files\RedDeerNorthSaskRegion.GDB@genesis-sde.sde\RedDeerNorthSaskRegion.GDB.POI' in_datavarTKS = r'U:\Tools\SDE_Connection_Files\RedDeerNorthSaskRegion.GDB@genesis-sde.sde\RedDeerNorthSaskRegion.GDB.Tracks' inLyrPOI = 'AEP Field Data Collection/Points of Interest' inLyrTKS = 'AEP Field Data Collection/Tracks' sdeconnection = r'U:\Tools\SDE_Connection_Files\RedDeerNorthSaskRegion.GDB@genesis-sde.sde' expChce = arcpy.GetParameterAsText(0) out_location = arcpy.GetParameterAsText(1) out_name = arcpy.GetParameterAsText(2) delFeat = arcpy.GetParameterAsText(3) # Create a FGDB to store copies data... arcpy.AddMessage('\n... Creating new File Geodatabase') arcpy.CreateFileGDB_management(out_location, out_name) # Choice of data to export if expChce == 'Points of Interest': inData = in_datavarPOI inLyr = inLyrPOI else: inData = in_datavarTKS inLyr = inLyrTKS # Copy features and attachments to new GDB... arcpy.AddMessage('\n... Coping features and attachments to the new File Geodatabase') arcpy.Copy_management(in_data=inData, out_data=os.path.join(out_location + os.sep + out_name +".gdb", out_name)) # Add New Featureclass to map and symbolize arcpy.AddMessage('\n... Adding Copied features to the current map') mxd = arcpy.mapping.MapDocument("CURRENT") df = mxd.activeDataFrame layer = arcpy.mapping.Layer(os.path.join(out_location + os.sep + out_name +".gdb", out_name)) arcpy.mapping.AddLayer(df, layer, "TOP") # List selected data... selFeat = [] with arcpy.da.SearchCursor(inLyr, 'OBJECTID') as cursor: for row in cursor: selFeat.append(row[0]) del cursor # Delete features that were not selected... sql = '' for sel in selFeat: sql += 'OBJECTID = ' + str(sel) + ' OR ' arcpy.SelectLayerByAttribute_management(out_name, selection_type='NEW_SELECTION', where_clause=sql[:-4]) arcpy.SelectLayerByAttribute_management(out_name, selection_type='SWITCH_SELECTION') arcpy.DeleteFeatures_management(out_name) # Choose to delete orginal features if delFeat == 'true': #Start editing print "Initiating editing" edit = arcpy.da.Editor(sdeconnection) edit.startEditing() edit.startOperation() #Test Cursor print "Testing cursor" Cursor = arcpy.da.UpdateCursor (inData, ["ObjectID"], sql[:-4]) for row in Cursor: print row[0] del row del Cursor #Stop/save edits edit.stopOperation() print "Stopping editing" edit.stopEditing("True") else: arcpy.SelectLayerByAttribute_management(in_layer_or_view=inLyr, selection_type='CLEAR_SELECTION') arcpy.AddMessage('\n... Finished coping features :)\n')
... View more
05-18-2018
11:02 AM
|
0
|
0
|
460
|
DOC
|
When I download this on windows 10 and extract the zip file the linkage between the script and the toolbox id broken. is there another copy that has the linkages built in? Also i am using arcmap 10.3. Shawn
... View more
05-17-2018
08:21 AM
|
0
|
0
|
6302
|
POST
|
Hey Luke, I certainly appreciate you looking into this but yes I have tried both senerios andhad the same results.
... View more
10-07-2015
12:43 PM
|
0
|
1
|
1051
|
POST
|
Hey Luke, I have not tried that. 'myRasLst[0] ' is the first item out of 'myRasLst' which is a list. I can run the tool from Arcmap Toolbox successfully pointing to the data sources.
... View more
10-07-2015
12:26 PM
|
0
|
3
|
1051
|
POST
|
so i have changhed the code to simplify the process and still get the error. code below... So i am sure there are no spaces and the data does exist. Any other thoughts would be most welcomed. oFolder = r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript" # the folder to copy to arcpy.CheckOutExtension("Spatial") # Execute FocalStatistics outFocalStatistics = FocalStatistics(r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\ec_1vtop25eg", NbrRectangle(100, 100, "CELL")) # Save the output outFocalStatistics.save(r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\mElev100Ha")
... View more
10-07-2015
12:09 PM
|
0
|
6
|
1051
|
POST
|
Thansk Dan, I updated the code to reflect trhe proper lines in the errors.
... View more
10-07-2015
10:38 AM
|
0
|
0
|
1051
|
POST
|
I have this script that run fine until it hits the final part FocalStatistics. Any ideas as to why it is failing? Below is the script and below the script is the error i get when i run the script. #-------------------------------------------------------------------------------
# Name:
# Purpose:
#
# Author: Shawn Keizer
#
# Created: 06/10/2015
# Copyright: (c) Government of Alberta
#-------------------------------------------------------------------------------
def focalStats(inRaster, neighbourHood, outRaster):
"""Calculates for each input cell location a statistic of the values within a specified neighborhood around it."""
arcpy.CheckOutExtension("Spatial")
outraster = FocalStatistics(inRaster, neighbourHood, "MEAN", "DATA")
outraster.save(outRaster)
def main():
AOI = r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\AOI.shp" # the area of interest to find the rasters for
rFolder = "//env.gov.ab.ca/GIS/GISdata/ttm/data/base/topo/25mDEM" # the folder containing the rasters to search from
oFolder = r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript" # the folder to copy to
desc = arcpy.Describe(AOI) # get the extent of the AOI
sExt = desc.extent
arcpy.env.workspace = rFolder
env.overwriteOutput = True
try:
arcpy.AddField_management(AOI,"UTM_Zone","TEXT","","", 255)
except:
print "UTM_Zone field already exist..."
myLyr = arcpy.MakeFeatureLayer_management(AOI, "AOI_Layer")
arcpy.CalculateUTMZone_cartography("AOI_Layer", "UTM_Zone")
for row in arcpy.da.SearchCursor("AOI_Layer", ["UTM_Zone"]):
myStr = str(row)
if "North American 1983 UTM Zone 11U" in myStr:
zone = "NAD_1983_UTM_Zone_11N"
else:
zone = "NAD_1983_UTM_Zone_12N"
del myLyr
myRasLst = []
for ThisRas in arcpy.ListRasters():
rDesc = arcpy.Describe(ThisRas)
rExt = rDesc.extent
sr = rDesc.spatialReference
if sr.name == zone:
if sExt.disjoint(rExt):
pass
else:
arcpy.AddMessage("Raster %s overlaps " % (ThisRas) + sr.name)
print "Raster %s overlaps " % (ThisRas) + sr.name
outFile = os.path.join(oFolder,ThisRas)
arcpy.Copy_management(os.path.join(rFolder,ThisRas),outFile)
myRasLst.append(os.path.join(oFolder,ThisRas))
if len(myRasLst) > 1:
for rs in myRasLst:
if rs != myRasLst[0]:
arcpy.Mosaic_management(rs,myRasLst[0],"MEAN")
focalStats(myRasLst[0], NbrRectangle(100, 100, "CELL"), os.path.join(oFolder,"mElev100Ha"))
if __name__ == '__main__':
import os, sys, arcpy
from arcpy import env
from arcpy.sa import *
main() Traceback (most recent call last): File "<string>", line 254, in run_nodebug File "C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\Test.py", line 67, in <module> main() File "C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\Test.py", line 61, in main focalStats(myRasLst[0], NbrRectangle(100, 100, "CELL"), os.path.join(oFolder,"mElev100Ha")) File "C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\Test.py", line 14, in focalStats outraster = FocalStatistics(inRaster, neighbourHood, "MEAN", "DATA") File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\sa\Functions.py", line 4830, in FocalStatistics ignore_nodata) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\sa\Utils.py", line 47, in swapper result = wrapper(*args, **kwargs) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\sa\Functions.py", line 4824, in wrapper ignore_nodata) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 498, in <lambda> return lambda *args: val(*gp_fixargs(args, True)) arcgisscripting.ExecuteError: ERROR 999999: Error executing function. Failed to create raster dataset Failed to execute (FocalStatistics). Message was edited by: Dan Patterson I took the liberty of formatting the code using Syntax highlighting in the >>> menu options, selecting Python
... View more
10-07-2015
10:21 AM
|
0
|
14
|
4827
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:25 AM
|