POST
|
Yes im not entirely sure on the workspace / folder implications of feature datasets so a bit of experimentation may be required around that and what I said may not be quite right. To prepare a 'proper' layer like what we use in arcmap, we would do this: newlayer = arcpy.mapping.Layer(path_to_shapefile_or_feature_class) Then you could use newLayer, as the variable for input to the tool. But, for a delete fields mgmt tool, you may as well skip that line and provide the full paths which will effectively create the layer as above behind the scenes for the purposes of processing as theres not much addiional properties required for this tool the layer method however could have advantages, for example if you wanted to apply a defquery before processing, we could now do this: newlayer = arcpy.mapping.Layer(path_to_shapefile_or_feature_class)
newlayer .definitionQuery = "[DISTRICT] = '1' AND [COUNTY] = 'ORANGE'"
next_process = processingTool(newlayer)
... View more
07-29-2019
06:32 AM
|
0
|
1
|
5876
|
POST
|
Firstly, "Placemarks" is not a folder, its what is called a 'feature dataset'. These are not meant to be used as folders, so if you are using them for this then that is not what they are created for. (Research these separately to find there uses) Then we go to https://pro.arcgis.com/en/pro-app/arcpy/functions/listfeatureclasses.htm and can see that it is expecting data in this structure: ListFeatureClasses ({wild_card}, {feature_type}, {feature_dataset})
It uses your environment path, that you set like this: ap.env.workspace = os.path.join("G:\\My Drive\\GDBs", gdb, "Placemarks") As feature datasets are not workspaces, we should instead set our path to:: ap.env.workspace = os.path.join("G:\\My Drive\\GDBs", gdb) Now, seeing the structure of ListFeatureClasses above, we could limit our results to the placemarks feature dataset, by calling list FCs as: featureclasses = ap.ListFeatureClasses(feature_dataset='Placemarks') Now ListFCs should return some data. (It was probably returning nothing with what you had above) Try to print it out (Good way to test your script and learn whats happening!), and it would provide a list of featurecalsses, as strings. (example: ['dataset1', 'dataset2'] To delete fields, as you say we should get a "reference" to the objects themselves instead of strings. Alternatively, we could just provide full paths to the dataset. To get this, we could do something like: full_path = os.path.join("G:\\My Drive\\GDBs", gdb, fc) and finally call the delete, using this path. (Instead of just a string that doesnt tell the tool where your data is) ap.DeleteField_management(full_path, remove_these)
... View more
07-29-2019
05:34 AM
|
2
|
3
|
5876
|
POST
|
I think ArcGIS itself just caches the entire script, .py or .pyc, the first time it is used in a session. Its really annoying for me as a developer, but there are work arounds when developing, such as rename your .tbx or .py file, so arcgis makes a new cache for this entirely new file. Apart from during development, it really shouldnt be an issue, as if you are making regular changes to part of your code, then that part should probably be a tool parameter!
... View more
07-19-2019
05:43 AM
|
0
|
0
|
3447
|
POST
|
I dont think you can sorry. A sources: https://community.esri.com/thread/88371 https://community.esri.com/thread/181642-arcpy-add-in-tool-deselect-tool Hopefully someone can help us!
... View more
07-12-2019
03:43 AM
|
0
|
0
|
170
|
POST
|
Did you get your adjusted sticks / section code sorted out?
... View more
07-04-2019
10:16 AM
|
0
|
0
|
2571
|
POST
|
When you write this code: for selFeature in lyr.getSelectionSet():
with arcpy.da.UpdateCursor(tbl, updateFieldsList) as updateCursor:
for updateRow in updateCursor:
if updateRow[0] == selFeature:
updateRow[1] = "YES"
updateCursor.updateRow(updateRow)
del updateCursor If you have more than 1 feature selected, the Cursor is running on all of the data and updating it. It will then update all of the data each time for every selected feature. You probable need something more like: if len(lyr.getSelectionSet()) > 0: update cursor stuff You have this in 2 places in your script, so it may be running your script many times more than is needed. Edit: Revised after re-reading the code more thoroughly!, you probably need something more along the lines of this, to stop the cursor going through all of your datasets: for selFeature in lyr.getSelectionSet():
with arcpy.da.UpdateCursor(tbl, updateFieldsList, where_clause="FID = '%s'" % selFeature) as updateCursor:
for updateRow in updateCursor:
if updateRow[0] == selFeature:
updateRow[1] = "YES"
updateCursor.updateRow(updateRow)
del updateCursor
... View more
07-01-2019
02:25 AM
|
1
|
0
|
357
|
POST
|
I always make my polylines like this, so no idea if this will help: polyline=arcpy.Polyline(array) row.setValue(shapename, polyline) For the offset, you probably need to include a spatial reference: spatref_epsg = 4326 #https://spatialreference.org/ref/epsg/wgs-84/ arcpy.CreateFeatureclass_management(out_path=folder, out_name=shapename,geometry_type='POLYLINE', spatial_reference=arcpy.SpatialReference(spatref_epsg))
... View more
07-01-2019
02:14 AM
|
1
|
1
|
473
|
POST
|
Instead of: arcpy.CalculateField_management(TempFC, "StoreID", Store) Try: arcpy.CalculateField_management(TempFC, "StoreID", '"%s"' % Store) I think when using strings instead of fields, you need to put the " " around the string, the same as if you were manually using field calculator.
... View more
06-21-2019
04:53 AM
|
2
|
1
|
2819
|
POST
|
Glad to hear! Now, I know nothing about multiprocessing, but I think by just making a small change, copying the "process" code into its own function, we could now implement multiprocessing easily, as the main script will build the list of processes to be run. Something like this: __author__ = 'Beast2'
import os
from operator import *
import time
import datetime
import arcpy
import sys
def create_output_directory(outdir):
if not os.path.exists(outdir):
try:
print('\nCreating new output directory!\n'+outdir+'\n')
os.makedirs(outdir)
except OSError:
print ("Creation of the directory failed" )
else:
print ("Directory Already Exists")
return True
def make_comparisons(value, minCheckType, minValue, maxCheckType, maxValue):
if minCheckType == None and maxCheckType == None: #We have no comparison!
result = "No comparison to make"
elif minCheckType == None: #query with just max being checked
result = maxCheckType(value, maxValue)
elif maxCheckType == None: #Query just checking minimums
result = minCheckType(value, minValue)
else:
result1 = maxCheckType(value, maxValue)
result2 = minCheckType(value, minValue)
if result1 == True and result2 == True:
result = True
else:
result = False
return result
def geoprocess(file_name, base_path, category, land_use):
output_directory_path = os.path.join(base_path, category[0], land_use[0])
print "Processing Land Use: %s" % land_use[0]
create_output_directory(output_directory_path)
ras_name = os.path.join(output_directory_path,("{0}_{1}{2}_{3}".format(category[0],file_name,land_use[0],".txt")))
fields = ['grid_code', 'Band1', 'Band2','Band3','Name',"Name2"]
with open(ras_name, 'wb') as txtfile:
arcpy.AddMessage("fields Writing For:{0}".format(land_use[0]))
txtfile.write(" {0} {1} {2} {3} {4} {5}\n".format(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5]))
with arcpy.da.UpdateCursor(category[1], fields) as cursor:
for row in cursor:
row_test_value = row[0]
#Check if the row value, is within the land use category
if make_comparisons(row_test_value, land_use[1], land_use[2], land_use[3], land_use[4]) == True:
row[5] = land_use[0]
print ("processing:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write(" {0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.close()
print ("Successfully save the:{0}".format(land_use[0]))
def main(file_name, base_path, categories, land_uses):
print "Processing to file name: %s" % file_name
print "Saving results to: %s" % base_path
print "Processing %s categories" % len(categories)
for category in categories:
print "Processing Category: %s" % category[0]
print "Category Input Data: %s" % category[1]
for land_use in land_uses:
#Implement Multi Processing Here to run all X processes at same time
a_process_that_could_run_parrallel_to_others = geoprocess(file_name, base_path, category, land_use)
if __name__ == '__main__':
try:
start_time = time.time()
#Categories we will process
category_1 = ['OOO', 'input_data']
category_2 = ['FFF', 'input_data']
category_3 = ['ddd', 'input_data']
categories = [category_1, category_2, category_3]
#Land Use category information
land_10 = ['LAND_10', gt, 0.1, lt, 0.2]
land_20 = ['LAND_20', ge, 0.2, lt, 0.5]
land_30 = ['LAND_30', ge, 0.5, None, None]
land_uses = [land_10, land_20, land_30]
#Other parameters
base_path = 'base_path'
file_name = 'file_name'
main(file_name, base_path, categories, land_uses)
print "serial processing took:",time.time() - start_time
print "serial processing is OK"
except:
print "Error in serial processing"
print arcpy.GetMessages()
print 'finished run: %s\n\n' % (datetime.datetime.now() - start_time)
... View more
06-19-2019
07:46 AM
|
0
|
2
|
548
|
POST
|
I did notice the issue, you are not actually using the result of the comparison. For example instead of: result = minCheckType(row[0], minValue)
row[5] = land_use
print ("processing_A:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write(" {0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
You should have done: result = minCheckType(row[0], minValue)
if result == True:
row[5] = land_use
print ("processing_A:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write(" {0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
Sadly did not spot this issue at first and am hopelessly addicted to python, so I rewrote your script here to show you how to use functions a bit more, there may be a bug as i didnt test it : __author__ = 'Beast2'
import os
from operator import *
import time
import datetime
import arcpy
import sys
def create_output_directory(outdir):
if not os.path.exists(outdir):
try:
print('\nCreating new output directory!\n'+outdir+'\n')
os.makedirs(outdir)
except OSError:
print ("Creation of the directory failed" )
else:
print ("Directory Already Exists")
return True
def make_comparisons(value, minCheckType, minValue, maxCheckType, maxValue):
if minCheckType == None and maxCheckType == None: #We have no comparison!
result = "No comparison to make"
elif minCheckType == None: #query with just max being checked
result = maxCheckType(value, maxValue)
elif maxCheckType == None: #Query just checking minimums
result = minCheckType(value, minValue)
else:
result1 = maxCheckType(value, maxValue)
result2 = minCheckType(value, minValue)
if result1 == True and result2 == True:
result = True
else:
result = False
return result
def main(file_name, base_path, categories, land_uses):
print "Processing to file name: %s" % file_name
print "Saving results to: %s" % base_path
print "Processing %s categories" % len(categories)
for category in categories:
print "Processing Category: %s" % category[0]
print "Category Input Data: %s" % category[1]
for land_use in land_uses:
output_directory_path = os.path.join(base_path, category[0], land_use[0])
print "Processing Land Use: %s" % land_use[0]
create_output_directory(output_directory_path)
ras_name = os.path.join(output_directory_path,("{0}_{1}{2}_{3}".format(category[0],file_name,land_use[0],".txt")))
fields = ['grid_code', 'Band1', 'Band2','Band3','Name',"Name2"]
with open(ras_name, 'wb') as txtfile:
arcpy.AddMessage("fields Writing For:{0}".format(land_use[0]))
txtfile.write(" {0} {1} {2} {3} {4} {5}\n".format(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5]))
with arcpy.da.UpdateCursor(category[1], fields) as cursor:
for row in cursor:
row_test_value = row[0]
#Check if the row value, is within the land use category
if make_comparisons(row_test_value, land_use[1], land_use[2], land_use[3], land_use[4]) == True:
row[5] = land_use[0]
print ("processing:""{0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.write(" {0:.8f} {1:.8f} {2:.8f} {3:.8f} {4} {5}\n".format(row[0],row[1],row[2], row[3],row[4],row[5]))
txtfile.close()
print ("Successfully save the:{0}".format(land_use[0]))
if __name__ == '__main__':
try:
start_time = time.time()
#Categories we will process
category_1 = ['OOO', 'input_data']
category_2 = ['FFF', 'input_data']
category_3 = ['ddd', 'input_data']
categories = [category_1, category_2, category_3]
#Land Use category information
land_10 = ['LAND_10', gt, 0.1, lt, 0.2]
land_20 = ['LAND_20', ge, 0.2, lt, 0.5]
land_30 = ['LAND_30', ge, 0.5, None, None]
land_uses = [land_10, land_20, land_30]
#Other parameters
base_path = 'base_path'
file_name = 'file_name'
main(file_name, base_path, categories, land_uses)
print "serial processing took:",time.time() - start_time
print "serial processing is OK"
except:
print "Error in serial processing"
print arcpy.GetMessages()
print 'finished run: %s\n\n' % (datetime.datetime.now() - start_time)py
... View more
06-18-2019
01:11 PM
|
1
|
1
|
1434
|
POST
|
I looked into it without conclusion my code seems good, this returns the correct values: FALSE, TRUE, TRUE from operator import *
minTolerance = 0.5
minType = ge #Greater Equal
maxTolerance = None
maxType = None
row1 = [0.4]
row2 = [0.5]
row3 = [0.6]
def make_comparisons(row, minValue, minCheckType, maxValue, maxCheckType):
if minCheckType == None and maxCheckType == None: #We have no comparison!
result = "No comparison to make"
elif minCheckType == None: #query with just max being checked
result = maxCheckType(row[0], maxValue)
elif maxCheckType == None: #Query just checking minimums
result = minCheckType(row[0], minValue)
else:
result1 = maxCheckType(row[0], maxValue)
result2 = minCheckType(row[0], minValue)
if result1 == True and result2 == True:
result = True
else:
result = False
return result
print make_comparisons(row1, minTolerance, minType, maxTolerance, maxType)
print make_comparisons(row2, minTolerance, minType, maxTolerance, maxType)
print make_comparisons(row3, minTolerance, minType, maxTolerance, maxType)
... View more
06-18-2019
12:08 PM
|
0
|
3
|
548
|
POST
|
I havent fully tested this, but heres a script that should hopefully show you how you could paramatise your "Tolerances", so you dont need to recode slightly different functions.
from operator import *
#We can use operators now
# gt = >
# ge = >=
#lt = <
#le = <=
minTolerance1= 0.10
minType1 = gt #Greater than
maxTolerance1 = 0.20
maxType1 = lt #Less than
minTolerance2 = 0.2
minType2 = ge #Greater Equal
maxTolerance2 = 0.5
maxType2 = lt #Less than
minTolerance3 = 0.5
minType3 = ge #Greater Equal
maxTolerance3 = None
maxType3 = None
row = [0.2]
def make_comparisons(row, minValue, minCheckType, maxValue, maxCheckType):
if minCheckType == None and maxCheckType == None: #We have no comparison!
result = "No comparison to make"
elif minCheckType == None: #query with just max being checked
result = maxCheckType(row[0], maxValue)
elif maxCheckType == None: #Query just checking minimums
result = minCheckType(row[0], minValue)
else:
result1 = maxCheckType(row[0], maxValue)
result2 = minCheckType(row[0], minValue)
if result1 == True and result2 == True:
result = True
else:
result = False
return result
print make_comparisons(row, minTolerance1, minType1, maxTolerance1, maxType1)
print make_comparisons(row, minTolerance2, minType2, maxTolerance2, maxType2)
print make_comparisons(row, minTolerance3, minType3, maxTolerance3, maxType3)
... View more
06-13-2019
08:09 AM
|
1
|
7
|
548
|
POST
|
Its a complex task to analyse your code, its very long as you say! But, I looked at GeoProcess 1, 2 and 3. They looked the same except you change a couple of variable: (You will know better what changes in each function) land_use='LAND_10' land_use='LAND_30' if (row[0] >= 0.20 and row[0] < 0.50): if (row[0] > 0.10 and row[0] < 0.20): etc etc. So, if the functions are doing the same thing, with slightly different variables, table names, etc. Then no, you should not code 10 more functions, you should add an extra variable to your code. def geoprocess(base_path,category,file_name,file, land_use, tolerance_minimum, tolerance_maximum): Then, you can reuse this function as you need by "Calling" it from the main script. e.g. process1 = geoprocess(base_path,category,file_name,file,'LAND_10', 0.2, 0.5)
process2 = geoprocess(base_path,category,file_name,file,'LAND_20', 0.1, 0.2)
process3 = geoprocess(base_path,category,file_name,file,'LAND_30', etc, etc)
process4 = geoprocess(etc)
... View more
06-12-2019
07:00 AM
|
1
|
1
|
494
|
POST
|
You are basically there and its looking good! Im probably using incorrect concepts for advanced/correct coding, but I like to keep it simpler! What you have basically created in your file functiontest2.py is what I like to call an interface or GUI. I would rename this file: harvest_report_commandline.py from HarvestTable import HarvestTableReport
import sys
HarvestThing = sys.argv[1]
HarvestTableReport(HarvestThing) Now to run this, you run it from cmd as you are probably familiar, passing in the sys argument. Often, I would then make another interface/gui file, so that I can easily test it inside an IDE. Something like this: harvest_report_python.py from HarvestTable import HarvestTableReport
HarvestThing = 'MY ID'
HarvestTableReport(HarvestThing) Further down the line, I would also make a .tbx file, so it works inside arcmap as a toolbox! Something like: harvest_report_ArcTool.py class harvestArcToolGUI(object): def __init__(self):
self.label = 'Harvest Report'
self.description = 'Get your report here!'
self.parameters = [
parameter('Harvest ID', 'HarvestID', 'String'),
]
def execute(self, parameters, messages):
'''
Reference the business logic captured in your object defined
separately in the module, preferably above.
'''
inHarvestID = parameters[0].value
###Tool Logic
execTool = HarvestTable.HarvestTableReport (inHarvestID)
return
Now, if you make changes to the orginal HarvestTable.py, all the other interfaces will be running the updated code.
... View more
05-30-2019
03:17 AM
|
1
|
0
|
257
|
POST
|
That Error Message only tells us the line it occurs, you seem to have left of the actual Error from the message.
... View more
05-30-2019
02:48 AM
|
1
|
3
|
1481
|
Title | Kudos | Posted |
---|---|---|
1 | 05-24-2011 07:53 AM | |
1 | 05-13-2013 07:11 AM | |
1 | 06-06-2017 09:40 AM | |
1 | 06-02-2015 09:13 AM | |
2 | 07-29-2019 05:34 AM |
Online Status |
Offline
|
Date Last Visited |
01-19-2023
10:21 AM
|