0

I had a problem solved here.

I had 2 features: polygons and points, both containing similar attributes(example: street name, number). Each polygon must have equivalent point added(same street name, same number), and I had to detect which polygons didn't have a point feature(or if they had one and the values are different, because of typos and such). The problem was solved by doing a spacial join and then filtering the ones with different values.

My new problem is that I need to run the script on an selection of features. Here you can find something that i plan to use. As I see it , before I run arcpy.MakeFeatureLayer_management I should use the arcpy.CopyFeatures_management feature, with arcpy.env.workspace = "in_memory" defined.

But what should I grab for the "selected_features" ? In that code, that does "pipes" define? Were does "in_memory" kick in?

import arcpy arcpy.env.overwriteOutput = True

from arcpy import env

arcpy.env.workspace = "in_memory"


# *make 'in memory' selection from featurelayer  arcpy.CopyFeatures_management (in_feat, out_feat)

selected_features = "D:/ALTE PROIECTE/script/cladiri adrese/Adrese_Output.shp"

sel_adrese = "D:/ALTE PROIECTE/script/cladiri adrese/selectadrese.shp"

arcpy.CopyFeatures_management ("D:/ALTE PROIECTE/script/cladiriadrese/Adrese_Output.shp", sel_adrese)



selected_features = "D:/ALTE PROIECTE/script/cladiri adrese/cladiri_Output.shp"

sel_poligon = "D:/ALTE PROIECTE/script/cladiri adrese/selectpoligon.shp"

arcpy.CopyFeatures_management ("D:/ALTEPROIECTE/script/cladiriadrese/cladiri_Output.shp", sel_poligon)


# *Make Feature Layers from memory selection

arcpy.MakeFeatureLayer_management(sel_adrese, "lyr_Adresa")

arcpy.MakeFeatureLayer_management(sel_poligon, "lyr_Poligon")


# *Do a spatial join

inputFC = "lyr_Poligon" joinFC = "lyr_Adresa" outputFC = "D:/ALTE PROIECTE/script/cladiri adrese/JoinPoligonAdresa.shp"

arcpy.SpatialJoin_analysis(inputFC, joinFC, outputFC)


# *Make a new Feature Layer that just contains the ones that need fixed

arcpy.MakeFeatureLayer_management("D:/ALTE PROIECTE/script/cladiriadrese/JoinPoligonAdresa.shp", "lyr_MySpatialJoinLayer", "(\"Strada\" <> \"DenumireAr\") >OR (\"Nr_postal\" <> \"Nr_Posta_1\")")



# *Create a new shapefile (or FGDB) with just the polygons that need fixed

arcpy.CopyFeatures_management("lyr_MySpatialJoinLayer", "D:/ALTE PROIECTE/script/cladiriadrese/Erori.shp")

That code will do the spacialjoin, but not on the features I selected. what I am missing out? I am using arcgis 10.2

2 Answers 2

1

IT seems that you have a typo in the following code as it is the only one wihtout spaces.:

arcpy.CopyFeatures_management ("D:/ALTEPROIECTE/script/cladiriadrese/cladiri_Output.shp", sel_poligon)

That said, I attempted to refactor your code a bit without any testing to make it more readable. IT seems to me you have some issues in layer definitions and defining the correct parameters for your spatial join such as "lyr_MySpatialJoinLayer", "lyr_Poligon", and "lyr_Adresa":

import arcpy arcpy.env.overwriteOutput = True

from arcpy import env

arcpy.env.workspace = "in_memory"


# *make 'in memory' selection from featurelayer  arcpy.CopyFeatures_management (in_feat, out_feat)

selected_features = "D:/ALTE PROIECTE/script/cladiri adrese/Adrese_Output.shp"
sel_adrese = "D:/ALTE PROIECTE/script/cladiri adrese/selectadrese.shp"

output_copy_features_ftr = "D:/ALTE PROIECTE/script/cladiriadrese/Adrese_Output.shp"
selected_features = "D:/ALTE PROIECTE/script/cladiri adrese/cladiri_Output.shp"
sel_poligon = "D:/ALTE PROIECTE/script/cladiri adrese/selectpoligon.shp"

arcpy.CopyFeatures_management (output_copy_features_ftr, sel_adrese)
arcpy.CopyFeatures_management (selected_features, sel_poligon)

# *Make Feature Layers from memory selection

arcpy.MakeFeatureLayer_management(sel_adrese, "lyr_Adresa")
arcpy.MakeFeatureLayer_management(sel_poligon, "lyr_Poligon")

# *Do a spatial join

inputFC = "lyr_Poligon"
joinFC = "lyr_Adresa"
outputFC = "D:/ALTE PROIECTE/script/cladiri adrese/JoinPoligonAdresa.shp"

arcpy.SpatialJoin_analysis(inputFC, joinFC, outputFC)


# *Make a new Feature Layer that just contains the ones that need fixed

arcpy.MakeFeatureLayer_management(outputFC,
                                  "lyr_MySpatialJoinLayer",
                                  "(\"Strada\" <> \"DenumireAr\") >OR (\"Nr_postal\" <> \"Nr_Posta_1\")")

# *Create a new shapefile (or FGDB) with just the polygons that need fixed
output_file_need_to_fix = "D:/ALTE PROIECTE/script/cladiriadrese/Erori.shp"
arcpy.CopyFeatures_management("lyr_MySpatialJoinLayer", output_file_need_to_fix)
1

Setting your workspace to in_memory will mean that for any new data file that you create, if a file path is not provided in the code, the data file will be saved in_memory, which is a temporary location. Working with data in_memory is faster than saving it to disk (such as C:\GISStuff), and it means less cleanup is needed on intermediate data files. In your code, since everything has a path provided, setting the workspace to in_memory doesn't do anything.

Your code has some unnecessary steps at the start of it, such as copying your shapefile to another shapefile. You then make a feature layer out of the new shapefile, which also doesn't accomplish anything for the desired task. You can simply use your orignal two shapefiles as your inputs for your spatial join.

There's a typo in your SQL statement (>OR). I also like to use FeatureClassToFeatureClass_conversion instead of arcpy.CopyFeatures_management, although the results should be the same.

Here's your code with a bit of cleaning up. It should produce the desired results:

import arcpy
arcpy.env.overwriteOutput = True

input_features = "D:/ALTE PROIECTE/script/cladiri adrese/Adrese_Output.shp"
selected_features = "D:/ALTE PROIECTE/script/cladiri adrese/cladiri_Output.shp"

# *Do a spatial join
outputFC = "D:/ALTE PROIECTE/script/cladiri adrese/JoinPoligonAdresa.shp"
arcpy.SpatialJoin_analysis(input_features, selected_features, outputFC)


# *Make a new Feature Layer that just contains the ones that need fixed
sql = '("Strada" <> "DenumireAr") OR ("Nr_postal" <> "Nr_Posta_1")'
arcpy.MakeFeatureLayer_management(outputFC, "lyr_MySpatialJoinLayer", sql)

# *Create a new shapefile (or FGDB) with just the polygons that need fixed
NewFCPath = r"D:/ALTE PROIECTE/script/cladiriadrese"
NewFCName = "Erori.shp"
arcpy.FeatureClassToFeatureClass_conversion ("lyr_MySpatialJoinLayer",  NewFCPath, NewFCName)
2
  • I have refined this code. The thing is I can't work with the original shapefiles. On one layer I must concatenate some columns, the other I must dissolve similar polygons. I have a new question thou, before I concatenate i run arcpy.MakeFeatureLayer_management then, after concatenate and other processing, I write the new shape to disk by arcpy.CopyFeatures_management. From my understanding arcpy.MakeFeatureLayer_management just makes a temporary copy of the layer, that must be written to disk if I want to save it. Is arcpy.MakeFeatureLayer_management using the in_memory functionality? Commented Dec 29, 2014 at 14:27
  • 1
    Nope, MakeFeatureLayer_management saves the data in its own place that is not the same as in_memory. To access this data you simply need to just use the string that you assigned to it. So to access a layer generated by, say, MakeFeatureLayer_management (shape.shp, "shapelyr"), you would maybe say FeatureClassToFeatureClass_conversion ("shapelyr", NewFCPath, NewFCName). I had a question in the same vein not long ago. gis.stackexchange.com/questions/127412/list-all-feature-layers Commented Dec 29, 2014 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.