Select to view content in your preferred language

Hi. Have problem with Creating buffer only in specific direction. What's wrong?

2055
16
02-11-2021 05:40 AM
konstantBrr
New Contributor II

import arcpy, math, gc

# Workspace, overwrite
arcpy.env.workspace = r"D:\testScript\My.gdb"
arcpy.env.overwriteOutput = True

# INPUTS
objects_input = "objects.shp" # must be polygons
objects = "objects_lyr.shp"
arcpy.MakeFeatureLayer_management(objects_input, objects)

# OUTPUTS, most temporal
result = "result.shp"
result_erase = "in_memory" + "\\" + "result_erase"
polygon = "in_memory" + "\\" + "polygon"
polygon_dissolve = "in_memory" + "\\" + "polygon_dissolve"

arcpy.CreateFeatureclass_management(arcpy.env.workspace, result, "POLYGON")

# Parameters
distance = 3 # distance for move in direction
direction = 90 # direction in degrees (90 is from north to south)
index = 0

# Set UpdateCursor
cur_objects = arcpy.da.UpdateCursor(objects, ("FID"))
for row_objects in cur_objects:
try:
fid = row_objects[0]
sql = '"FID" = ' + str(index)
index += 1

# Initialize lists
lines_list = []
lines_created = []

# Select current feature
arcpy.SelectLayerByAttribute_management(objects, "NEW_SELECTION", sql)
vertexes = "in_memory" + "\\" + "vertexes"

# Convert object to vertexes
arcpy.FeatureVerticesToPoints_management(objects, vertexes, "ALL")
index_vertex = 0

# Set SearchCursor for vertexes
cur_vertexes = arcpy.da.SearchCursor(vertexes, ("SHAPE@XY"))
for row_vertexes in cur_vertexes:
vertex_coords_x = row_vertexes[0][0]
vertex_coords_y = row_vertexes[0][1]

# Define points coordinates
point_move_x = vertex_coords_x - (distance) * math.cos(math.radians(direction))
point_move_y = vertex_coords_y - (distance) * math.cos(math.radians(90 - direction))

# Make list of points
new_line = ([[vertex_coords_x, vertex_coords_y], [point_move_x, point_move_y]])
lines_list.append(new_line)

# From second cycle
if index_vertex > 0:
lines_vertexes = ([[vertex_coords_x, vertex_coords_y], start_line])
lines_ends = ([[point_move_x, point_move_y], end_line])
lines_list.append(lines_vertexes)
lines_list.append(lines_ends)
start_line = [vertex_coords_x, vertex_coords_y]
end_line = [point_move_x, point_move_y]
index_vertex = index_vertex + 1

# Cycle that makes polylines from points
for lines_step in lines_list:
lines_created.append(arcpy.Polyline(arcpy.Array([arcpy.Point(*sour) for sour in lines_step])))

arcpy.FeatureToPolygon_management(lines_created, polygon)
arcpy.AggregatePolygons_cartography(polygon, polygon_dissolve, 1)

# Final editing
arcpy.Erase_analysis(polygon_dissolve, objects, result_erase)
arcpy.Append_management(result_erase, result, "NO_TEST")
arcpy.Delete_management("in_memory")
arcpy.Delete_management(vertexes)
start_line = []

# Clear selection, memory and deleting temps
arcpy.SelectLayerByAttribute_management(objects, "CLEAR_SELECTION")
print ("Object number: " + str(index - 1) + " -- done.")
gc.collect()


# Catch errors
except Exception as e:
pass
print ("Error:")
print (e)
print ("\n")
index += 1

 

 

_______________________________________________________

Traceback (most recent call last):
File "D:/testScript/myDBpy.py", line 10, in <module>
arcpy.MakeFeatureLayer_management(objects_input, objects)
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 6520, in MakeFeatureLayer
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset objects.shp does not exist or is not supported
Failed to execute (MakeFeatureLayer).

0 Kudos
16 Replies
DavidPike
MVP Frequent Contributor

Your have result ='result.shp', then in create feature class you're using an FGDB environment. This results in the name error.  Where did you get this script from?

0 Kudos
konstantBrr
New Contributor II
0 Kudos
by Anonymous User
Not applicable

Your error message says that there is a problem in line 10, which @DavidPike addressed in his replies.

File "D:/testScript/myDBpy.py", line 10, in <module>

Next bit of info in the error message says that 'objects.shp' is your problem

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset objects.shp does not exist or is not supported
Failed to execute (MakeFeatureLayer).

# INPUTS
objects_input = "objects.shp" ### IF ITS IN A sde/FGDB, THERE IS NO EXTENSION

# Like this
objects_input = 'objects'

I'd HIGHLY suggest to spend some time getting familiar with a debugger and step through your code- especially code taken from SO.  Looking at your script, you have some cyclic selections going on that may have gotten mixed up when you copied from SO and fit it to your data.  Like this updateCursor- you set the fid but don't use it anywhere and use the sql query to query the same features 'objects' that the cursor is using.

# Set UpdateCursor
cur_objects = arcpy.da.UpdateCursor(objects, ("FID"))
for row_objects in cur_objects:
try:
fid = row_objects[0]
sql = '"FID" = ' + str(index)
index += 1

# Initialize lists
lines_list = []
lines_created = []

# Select current feature
arcpy.SelectLayerByAttribute_management(objects, "NEW_SELECTION", sql)
vertexes = "in_memory" + "\\" + "vertexes"

... Shouldn't be using the updateCursor either if it is not updating anything.  If you need to iterate over each feature in the fc, then there are probably better ways of doing so like maybe creating a range for the number of features and passing that number into your SelectLayerByAttributes function.  It looks like you are just going 0: .... for the function.

Rename your variables so they make sense and so that anyone reading your code can tell what they are.  Same with Featureclasses, shapefiles, databases, etc.  It's just good practice and when you have to revisit the script you wont be scratching your head trying to figure out what 'objects' are.

And formatting code for the forum is clicking on the </> icon in the toolbar above the textarea, pasting the into the window and the most important part is selecting python from the dropdown.

Spend some time with your debugger- it'll help you get familiar with your code.

DanPatterson
MVP Esteemed Contributor

read the code formatting link I posted very early on in this post

And you can't have a shapefile in a geodatabase, only in a folder.  

This has been pointed out several times.


... sort of retired...
konstantBrr
New Contributor II

Thank you all for your support! My problem is solved! Ijust change parameters of output shp-file.
Sorry for my incorrect and long topic. This is cause I just registered at community.

0 Kudos
konstantBrr
New Contributor II
# OUTPUTS, most temporal 
out_path=r"D:\testScript"
out_name="Hello.shp"
geometry_type = "POLYGON"
template = "study_quads.shp"
has_m = "DISABLED"
has_z = "DISABLED"
Myres = "Myres.shp"
arcpy.Delete_management(out_name) 
out_name_erase = "in_memory" + "\\" + "out_name_erase" 
polygon = "in_memory" + "\\" + "polygon" 
polygon_dissolve = "in_memory" + "\\" + "polygon_dissolve"
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type)
0 Kudos