POST
|
Heres another example of what I'm talking about. I am trying to convert a shapefile to a feature class, I also have a geojson file with the field names as I would like them to appear in the output feature class. The `arcpy.FeatureClassToFeatureClass_conversion` functionality accepts a field map, so I tried to build one by looping through a geopandas geodataframe of the geojson file (to set my output field names) and by adding the shapefile to a fieldMappings object, and replacing the field map for each iteration: import arcpy import geopandas file_path = 'damage_results/Culverts/Culverts.shp' original_fields = arcpy.ListFields(file_path) original_fields = original_fields[2:] gdf = geopandas.read_file('damage_results/Culverts/result_culverts.json') new_field_names = gdf.columns[:-1].to_list() fms = arcpy.FieldMappings() fms.addTable(file_path) for i,field in enumerate(original_fields): print i fm = fms.getFieldMap(i) out_field = fm.outputField out_field.name = new_field_names fm.outputField = out_field fms.replaceFieldMap(i, fm) for fm in fms: print fm.outputField.name arcpy.FeatureClassToFeatureClass_conversion(file_path, 'intermediate_damage.gdb', 'Culverts', field_mapping = fms) The second `for` loop is to verify that the output field names have been written properly, and it seems from within the script that they are. However when I open the Culverts feature class the field names are unchanged from the shapefile, which is rather infuriating. Does the output field attribute in the `FieldMap` object actually work? Or is this a bug that ESRI needs to fix?
... View more
05-14-2019
08:40 PM
|
0
|
0
|
301
|
POST
|
I am trying to use the `FieldMap` and the `FieldMappings` classes in `arcpy` in the context of a spatial join. I have two line layers as feature classes. These are `Roads` and `Culverts`. Roads has a copy of the `OBJECTID` field called `ID`. I am spatially joining `Roads` to `Culverts`, but I would like to control the name of the joined `ID` field from the road layer. Here's some python code I am using: fm = arcpy.FieldMap() fms = arcpy.FieldMappings() fm.addInputField('Road', 'ID') out_field = fm.outputField out_field.name = 'ROID' fm.outputField = out_field fms.addFieldMap(fm) arcpy.SpatialJoin_analysis(target_features="test.gdb/Culvert", join_features="test.gdb/Road", out_feature_class="test.gdb/join", join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_ALL", field_mapping =fms, match_option="CLOSEST", search_radius="", distance_field_name="") Unfortunately I get a result where the field is called `ID` (just like in the original road layer) and the values are all 0 in this field (which is incorrect). I have a couple of questions: - how do I control the name of the output field using the `FieldMap` and `FieldMappings` classes? - Why are the attributes all 0 and how can I fix that? - Can I use the `OBJECTID` of the road layer directly without having to make a copy called `ID`, and using the `FieldMap` and `FieldMappings` classes THanks.
... View more
05-13-2019
07:51 PM
|
0
|
1
|
413
|
POST
|
I am trying to use arcpy.ApplySymbologyFromLayer_management. i have 2 dataframes in my mxd and preauthored .lyr files which I am not loading into my session, but only referring to when I want to apply the symbology. I shift to the dataframe of interest using mxd.activeView = df. However when I attempt to run arcpy.ApplySymbologyFromLayer it will not change the symbology of the layers in my secondary dataframe(s). It only changes the symbology in my primary dataframe. Also I have noticed that there is no argument in arcpy.ApplySymbologyFromLayer which indicates what dataframe it is to operate on. Is this a bug in arcpy which would render arcpy.ApplySymbologyFromLayer obsolete for map documents with multiple dataframes or am I doing something wrong in my script?
... View more
11-24-2018
04:39 AM
|
0
|
1
|
333
|
POST
|
Thanks for taking a look. I will address items 2 and 3 from your post. 1 and 4 are important and relevant, however I don't believe they are causing the issue at hand. My responses are as follows: 2. I am doing this because, in my humble opinion, the following is quite redundant (alternate to line 35): extraction_buffer = arcpy.Buffer_analysis(in_features=input_shapefile, out_feature_class = 'extraction_buffer', buffer_distance_or_field=mask_extraction_buffer_size_as_string, line_side="FULL", line_end_type="FLAT", dissolve_option="ALL", dissolve_field="", method="PLANAR") I don't like the idea of having to specify both the out_feature_class as 'extraction_buffer' while instantiating the variable in my script. This result being intermediate, it's name should be completely irrelevant. I am intentionally omitting the output file name and letting arcpy choose an arbitrary name for it. I only want to keep a reference to it such that I can use it in a subsequent geoprocessing operation. Of course I could do this instead: arcpy.Buffer_analysis(in_features=input_shapefile, out_feature_class = 'extraction_buffer', buffer_distance_or_field=mask_extraction_buffer_size_as_string, line_side="FULL", line_end_type="FLAT", dissolve_option="ALL", dissolve_field="", method="PLANAR") But then I would not be able to place it as part of some internal pythonic data structure if I wanted to (such as a list or dict). I'd always have to refer to it by 'extraction_buffer' as a string which is somehow annoying to me.I want the thing as an object. I'm sure arcpy has a way to do this? Aren't arcpy.FeatureSet and arcpy.Raster class types which represent feature classes or rasters? I used those in lines 20 and 21... 3. It's quite likely that the resultobject has no spatial reference. If so then I would ask the following: how can I set the results of geoprocessing operations such that I don't have to worry about their file name and extension, and such that they retain all their geospatial properties including projection/spatial reference? Sorry for the long-winded response, but I hope it clarifies things. Thanks!
... View more
08-07-2018
11:21 AM
|
0
|
1
|
1138
|
POST
|
resampled_raster is on disk. I want for reclassed_raster to get dumped to disk also. Stepping out of the script to run an entire arcmap session for a simple reclass is definitely something I'd like to avoid. I'd like to try to resolve the source of this problem. Line 76 takes hours to run, and once its done I expect line 123 also to take hours. Furthermore I will have to make sure this script can run for several input datasets. I have to start the script and let it run overnight. Interrupting the whole process by reclassifying in ArcMap is not an option I'm afraid.
... View more
08-07-2018
02:00 AM
|
0
|
0
|
1138
|
POST
|
Hi Dan, thanks for your response. I have attached the full scripts to my original question. The problematic line is line 100. test.py is the main script. force_zonal.py is called by test.py at line 123 but I really don't think it will have an impact on the reclassify problem. I've just included here for completeness. If you cannot find the scripts, please let me know and I'll provide another link for you to download them.
... View more
08-07-2018
12:31 AM
|
0
|
2
|
1138
|
POST
|
I have a raster which originates from a large tiff file being clipped and resampled. The resulting raster resampled_raster retains the same projection and opens up just fine in arcmap. I would like to reclassify resampled_raster using arcpy and assign the result to a variable named reclassed_raster. Both resampled_raster and reclassed_raster are not string variables but rather variables of type <class 'arcpy.arcobjects.arcobjects.Result'> which should be fine for my purposes. I have been flinging these objects around in my script as geoprocessing inputs and outputs without any problems thus far. The following command, when executed from cmd: reclassed_raster = arcpy.gp.Reclassify_sa(resampled_raster, "Value", "1 1;2 1;3 1;4 1;5 1;NODATA 0", "reclass", "DATA") throws an error: return lambda *args: val(*gp_fixargs(args, True)) arcgisscripting.ExecuteError: ERROR 999999: Error executing function. General function failure This spatial reference object cannot be defined from the available information. Failed to execute (Reclassify). Strangely, when I try to reclass my raster (the same exact one) through an arcmap session I don't have any problems, it reclasses just fine. I also googled the error to see if any potential solutions or workarounds exist and I came across this link, which claims the following: Cause This is expected behavior, as there are certain naming restrictions set for the grid raster format. The naming restrictions include special characters, which are not supported by the grid raster format. Special characters may include accents and umlauts as well as non-alphanumeric characters. Solution or Workaround Rename the folder to ensure the path name of the raster dataset does not contain special characters, or move the raster file to a directory containing no special characters. But in my case my directory contains no special characters. I tried different possibilities such as adding the name of the output raster in my function call, not assigning the result to a `reclassed_raster` variable and other obvious variations, but to no avail. I need to be able to reclass this raster through cmd so I don't have to babysit my script. What could be the problem here? Why does it run OK in arcmap but not in my script through cmd?
... View more
08-06-2018
09:15 PM
|
0
|
9
|
1708
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|