POST
|
I had this problem as well, it turns out that I had used another layer as a "template" when creating this layer, and it copied the Shape.STLength() field to Shape_STLength__. I was doing this in a python script so I just did a quick test to see if the Length or Area columns got copied funny and if they did remove them. The Shape_STLength__ (and Shape_STArea__) field is not used by anything, it just causes grief. The real field Shape.STLength() is not actually a field in the database just a pointer to the length of the actual shape column.
... View more
12-12-2014
07:27 AM
|
0
|
0
|
356
|
POST
|
Ben, It might be easier in this case (and probably faster) to simply us a feature layer and your calculate field. I would try something like this:
import arcpy
fc = r"c:\Test_Orient.shp"
feildName = "ORIENT"
expression = "rand_num()*360"
codeblock = """def rand_num():
import random
return random.random()"""
arcpy.MakeFeatureLayer_management(fc, "TEST_ORIENT_LAYER", fieldName + " = 0")
arcpy.CalculateField_management("TEST_ORIENT_LAYER", fieldName, expression, "PYTHON_9.3", codeblock)
Using make feature layer makes an "in memory" pointer to the layer, much like adding the layer to an MXD does, it also happens to allow you to set a where clause. Once you have this (I called it TEST_ORIENT_LAYER) then you can access it in almost any toolbox, as almost all of them take in a Feature Layer or a Feature Class (shape files included). Check out the documentation here: ArcGIS Help 10.1 As an aside, in my testing, until you want to calculate more than about 5 columns per row, it is faster to use a method like this as the Calc Field tool is faster than even a Data Access cursor until about 5 columns. After 5 columns, you should probably open the cursor and do the calculations yourself.
... View more
07-28-2014
06:03 AM
|
0
|
1
|
645
|
POST
|
Antonio, I have had very similar problems. One way that usually makes this work (not that it is a very good way) is to import your toolboxes (in the correct order) from the python command window in ArcMap. This is not ideal, but usually gets things to work. I have a similar setup for some tools that I have create, lets call them BaseTools and CompositeTools. The CompositeTools call tools from BaseTools, but BaseTools are all stand alone, they do not have dependencies on each other, nor on any tools other than Esri tools. These tools are built within a Python site package, and distributed using this technique. This makes the PYT files appear as system toolboxes in ArcMap in 10.2, it doesn't work in 10.0 or 10.1, but it will make things consistent, and you can easily add the PYT files by calling a custom function on the site package: see below. To help make this work consistently here is what I do: Add a method to the root of my site package called "add_toolbox" The job of this method is to figure out where the PYT file is, check to see if it is already imported (remove it if it is) and then import it, so it looks like this (I put the other functions that I call at the end, I couldn't get them to paste here correctly) (also, I am importing arcpy within my function, but that is b/c of dependencies that I have so you may not do that there):
def add_toolbox():
import arcpy
## List toolboxes and see if eaglepods6 is already installed
eagle_pods_6_toolbox = arcpy.ListToolboxes("basetools")
if len(eagle_pods_6_toolbox) == 1:
arcpy.RemoveToolbox(_get_composite_toolbox_path())
arcpy.RemoveToolbox(_get_toolbox_path())
arcpy.AddToolbox(_get_toolbox_path())
arcpy.AddToolbox(_get_composite_toolbox_path())
return
This is where it gets really strange, so this works great because I now have a function to load my toolboxes, this makes it easy to call from the command line, or from inside a python script that is using my tools, but it does not fix the one tool depending on the other tool problem. To get that to work I had to add the following to my CompositeTools PYT file:
import arcpy
import my_library
my_library.add_core_toolbox()
This function "add_core_toolbox" is almost the same as the above add toolbox function, but instead of adding both toolboxes (Composite and Base) it only adds the Base. This ensures that when ArcMap is opening and loading PYTs, when it gets to the Composite one, it will force the Base one to load. So my add_core_toolbox looks like this:
def add_core_toolbox():
import arcpy
## List toolboxes and see if eaglepods6 is already installed
eagle_pods_6_toolbox = arcpy.ListToolboxes("basetools")
if len(eagle_pods_6_toolbox) == 1:
arcpy.RemoveToolbox(_get_toolbox_path())
arcpy.AddToolbox(_get_toolbox_path())
return
I always make sure that I list the toolboxes, and remove the tool if it already exists. This was added after trial and error, and seems to help ensure that the toolbox is loaded only once, even though they are always coming from the same path (in site packages). I have never been able to get this to work outside of the special folder in site packages that Esri uses to load system tools. But using the code above, it does seem to work well in 10.0 - 10.2, which is nice. Other functions used above:
def _get_toolbox_path():
import os.path
return os.path.join(os.path.dirname(__file__), "ESRI", "TOOLBOXES", "Base Tools.pyt")
def _get_composite_toolbox_path():
import os.path
return os.path.join(os.path.dirname(__file__), "ESRI", "TOOLBOXES", "Composite Tools.pyt")
Essentially there is an issue with the way/order in which PYT files are read in by the system, Esri already knows about it and is working on a fix, but in the meantime, this is the only way I know of to get interdependent PYT files to work.
... View more
07-21-2014
06:00 AM
|
1
|
0
|
340
|
POST
|
Glad I could help! Let me know if you ever need anything else!
... View more
07-17-2014
09:08 AM
|
0
|
0
|
1713
|
POST
|
Another option that would be great is for Esri to simply include pip and Easy Install. This would make it easy for people to write scripts in Python or even BAT files that could add any 3rd party libraries needed. Most Python distributions (excluding windows) come with pip and Easy Install ready to go, it seems like something Esri could add that would make it more "pythonic" and much easier for anyone to require additional libraries or dependencies.
... View more
07-17-2014
09:01 AM
|
2
|
3
|
611
|
POST
|
Jack, Yeah it is possible. A simple version is not too hard (I have updated the code) but it does start to get complicated with different data frames and different bookmarks. So, that said, what I made it do is export each data frame, with each bookmark that applies to it. The file names are calculated based on the name of the data frame and the bookmark. I have attached the tool and have updated the gist at: An extension of export_mxd.py that provides an ESRI toolbox that does the same thing as the export_mxd.py script. I will add this to my library (pzpy) when I have some time after the UC. You can check it out here: http://pzpy.guru Christopher
... View more
07-16-2014
11:23 AM
|
1
|
2
|
1713
|
POST
|
Jack, I put together a quick script that does this just this morning! I am doing it for PDF, but you can easily do it for JPEG as well. Just for kicks I went ahead and converted this into a PYT file. You should be able to take this file and open it in ArcMap like any toolbox tool (as long as you are using 10 or higher). All you need to do is specify the folder and then the type of export you want, in your case JPEG and let it go. It generates the JPEGs in the same folder as the MXD and with the same name. You can change that around though. Check it out here: An extension of export_mxd.py that provides an ESRI toolbox that does the same thing as the export_mxd.py script. I have also attached the pyt file for you. It should behave just like a normal toolbox once you download it, just browse to it in the catalog view of your ArcGIS for Desktop and double click on the tool in the toolbox. Specify the folder path you want, and the type of export. Christopher
... View more
07-15-2014
10:51 PM
|
2
|
5
|
1713
|
POST
|
Hayley, I know you got it working, but I went ahead and took a pass at a script anyway. Basically I created a dictionary that holds the "Park ID" values where the Park ID is the key, and stored the number of records that we have encountered for that park as the value. What this allows the code to do is keep track of how many times it has encountered a particular Park ID and use that number to create the new unique ID. It is not smart enough to updated only null records, instead it will just rebuild all the Id's, that may not be what you want in the end. Here is the code to put in the "Pre-Logic Script Code": global id_value_dict id_value_dict = dict() def calc_new_id(base_id_value): global id_value_dict if base_id_value in id_value_dict: id_value_dict[base_id_value] += 1 else: id_value_dict[base_id_value] = 1 return str(base_id_value) + "_" + str(id_value_dict[base_id_value]).zfill(3) You can also check out a gist of it here: A quick script to create a unique ID based on the value of a different column. For example, the column value might have… Hope that helps! Christopher
... View more
07-15-2014
10:22 PM
|
1
|
1
|
320
|
Title | Kudos | Posted |
---|---|---|
1 | 07-21-2014 06:00 AM | |
1 | 07-15-2014 10:22 PM | |
2 | 07-17-2014 09:01 AM | |
1 | 07-16-2014 11:23 AM | |
2 | 07-15-2014 10:51 PM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|