How intersect layers and exporting to shapefile with ArcPy

1149
5
06-16-2017 04:25 PM
VictorQuiroz
New Contributor

Hi every one I need some help!!

I want to intersect a few shapefile multipoint with other shapefile polygon, and the selection points intersected I need to export to shapefile with diferent name, but I get a nasty error.

Somebody can help me to fix the problem?

This my script:

import arcpy
from arcpy import env

arcpy.env.overwriteOutput = True

Ruta1_entrada = r'D:\\SedesolVQB\\'
Shp1 = 'n_loctdico_0417_09_.shp' #<---Shp multipoint
Shp1_entrada = Ruta1_entrada + Shp1

Ruta2_entrada = r'D:\\SedesolVQB\\'
Shp2 = 'Areas_intersectan.shp' #<----Shp Polygon
Shp2_entrada = Ruta2_entrada + Shp2

Ruta3_salida = r'D:\\SedesolVQB\\Resultado_final\\'
Shp_final = 'Resultado_fina.shp'
Resultado_final = Ruta3_salida +Shp_final

#Convert to layer
arcpy.MakeFeatureLayer_management(Shp1_entrada,"Tiendas.lyr")
#Select by location
arcpy.SelectLayerByLocation_management("Tiendas.lyr","INTERSECT",Shp2_entrada, "","NEW_SELECTION")
Num_seleccionados = arcpy.GetCount_management(Shp1)

print "Puntos seleccionados: " + str(Num_seleccionados)

However, I get the next error message:

Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "D:\SedesolVQB\Proyectos\proyecto_MODELBUILDER_DGAAE\Selectbylocation.py", line 14, in <module>
arcpy.env.overwriteOutput = True
AttributeError: 'str' object has no attribute 'overwriteOutput

0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

you didn't set the workspace before you set it to overwrite

arcpy.env.workspace = r'C:\Somefolder'

arcpy.env.overwriteOutput = True

env class

environment settings

0 Kudos
VictorQuiroz
New Contributor

Thank a lot for answer, I did what you said but I get another error I don't understand what exactly means

import arcpy
from arcpy import env

arcpy.env.workspace = r'D:\\SedesolVQB\\Salida\\'
arcpy.env.overwriteOutput = True

Ruta1_entrada = r'D:\\SedesolVQB\\'
Shp1 = 'n_loctdico_0417_09_.shp' #<---Shp multipoint
Shp1_entrada = Ruta1_entrada + Shp1

Ruta2_entrada = r'D:\\SedesolVQB\\'
Shp2 = 'Areas_intersectan.shp' #<----Shp Polygon
Shp2_entrada = Ruta2_entrada + Shp2

Ruta3_salida = r'D:\\SedesolVQB\\Resultado_final\\'
Shp_final = 'Resultado_fina.shp'
Resultado_final = Ruta3_salida +Shp_final

#Convert to layer
arcpy.MakeFeatureLayer_management(Shp1_entrada,"Tiendas.lyr")
#Select by location
arcpy.SelectLayerByLocation_management("Tiendas.lyr","INTERSECT",Shp2_entrada, "","NEW_SELECTION")
Num_seleccionados = arcpy.GetCount_management(Shp1)

print "Puntos seleccionados: " + str(Num_seleccionados)

#Export selection
arcpy.FeatureClassToFeatureClass_conversion(Shp1,Ruta3_salida, Rusultado_final)

File "D:\SedesolVQB\Proyectos\proyecto_MODELBUILDER_DGAAE\Selectbylocation.py", line 14, in <module>
arcpy.env.workspace = r'D:\\SedesolVQB\\Salida\\'
AttributeError: 'str' object has no attribute 'workspace'

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Victor...

You seem to be getting a string before you are setting it to a workspace.  Examine the code

import arcpy

arcpy.env.workspace = r"c:\temp"

arcpy.env.overwriteOutputs = True

arcpy.env
Out[4]: <arcpy.geoprocessing._base.GPEnvironments.<locals>.GPEnvironment at 0x26fab40d470>

print(arcpy.env.overwriteOutputs)
True
‍‍‍‍‍‍‍‍‍‍‍

Try that  and skip the double-backslashes if you are using raw notation. and you shouldn't end a path with a backslash generally

It can cause issues, the least of which is an error, the most of which is that the assumption that your path will end in a backslash won't be realized.

arcpy.env.workspace = r"c:\temp\"             # No Good
  File "<ipython-input-6-f3b303a774e7>", line 1
    arcpy.env.workspace = r"c:\temp\"
                                     ^
SyntaxError: EOL while scanning string literal


arcpy.env.workspace = r"c:\\temp\\"           # we will cleverly try to get it to end with \

print(arcpy.env.workspace)                    # didn't work
c:\Temp

arcpy.env.workspace = "c:/temp"               # alternate style

print(arcpy.env.workspace)                    # as expected
c:/Temp

arcpy.env.workspace = "c:/temp/"              # trying to be clever with alternate

print(arcpy.env.workspace)                    # a bust in again
c:/Temp

# Lesson?  don't try to make a path end with a backslash... append to a path
#     after you create the initial one
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

And your paths will be messy in any event.  Without getting fancy, consider the simplest solution

Ruta3_salida = r'D:\\SedesolVQB\\Resultado_final\\'

Shp_final = 'Resultado_fina.shp'

Resultado_final = Ruta3_salida +Shp_final

Resultado_final            # can we get any more backslashes here
Out[16]: 'D:\\\\SedesolVQB\\\\Resultado_final\\\\Resultado_fina.shp'

Ruta3_salida = r'D:\SedesolVQB\Resultado_final'   # raw notation and dump the last \


Shp_final = r'\Resultado_fina.shp'   # use raw notation and slip a backslash here

Resultado_final = Ruta3_salida +Shp_final

Resultado_final              # Easier to read???
Out[21]: 'D:\\SedesolVQB\\Resultado_final\\Resultado_fina.shp'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Which...for both python 2.7 and 3.5/6/7  shows that all those extraneous backslashes aren't necessary.

import os

pth = r"c:\\temp\\"

pth
Out[30]: 'c:\\\\temp\\\\'

os.path.exists(pth)
Out[31]: True

os.path.realpath(pth)
Out[32]: 'c:\\temp'
0 Kudos
VictorQuiroz
New Contributor

Thanks for answer, I will do what you say, I going to study my script until solve it =)

0 Kudos