Select to view content in your preferred language

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

2222
16
02-11-2021 05:40 AM
konstantBrr
Emerging Contributor

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

I've not read the entire script, also please consider formatting it to make it easier to read.

I can however see a workspace envionment set as an FGDB, then referencing a shapefile called "objects.shp".

You wont have a shapefile in that workspace, so give the full path to the shapefile.

konstantBrr
Emerging Contributor

import arcpy, math, gc

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

# INPUTS
objects_input = r"D:\testScript\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 = 9 # 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

0 Kudos
BlakeTerhune
MVP Regular Contributor

Please mention any error messages or results that the script is currently producing.

konstantBrr
Emerging Contributor

Traceback (most recent call last):
File "D:/testScript/myDBpy2.py", line 18, in <module>
arcpy.CreateFeatureclass_management(arcpy.env.workspace, result, "POLYGON")
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 1807, in CreateFeatureclass
raise e
ExecuteError: ERROR 000354: The name contains invalid characters
Failed to execute (CreateFeatureclass).

0 Kudos
by Anonymous User
Not applicable

You also have an updateCursor that probably can be a search cursor since it doesn't look like it is ever updating the row.

Another tip that I see is that your objects is pointing to 'objects_lyr.shp'.  Since it is only a view (think of it as a temporary copy of the original data that you can do stuff to), you don't need the .shp at the end.  It can live and be referenced as 'objects_lyr' and you can also reference it through assigning the MakeFeatureLayer to a variable.   This doesn't effect the code at all, but more of a FYI. 

 

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

 

 to

 

# INPUTS
objects_input = "objects.shp" # must fix this per David's response
objectOutName = "objects_lyr.shp"
objLyr = arcpy.MakeFeatureLayer_management(objects_input, "objects_lyr")

 

then replace 'objects' with 'objLyr' where needed so its more readable and people know that it is referring to a Layer. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - GeoNet, The Esri Community would help with readability 


... sort of retired...
0 Kudos
konstantBrr
Emerging Contributor

Thank You. I will try it!

0 Kudos
konstantBrr
Emerging Contributor

Still not workinng! May be cause  I'm verry junior in python coding for arcgis😔

 

import arcpy, math, gc

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

# INPUTS
objects_input = r"D:\testScript\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 = 9 # 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

0 Kudos
konstantBrr
Emerging Contributor

Traceback (most recent call last):
File "D:/testScript/myDBpy2.py", line 18, in <module>
arcpy.CreateFeatureclass_management(arcpy.env.workspace, result, "POLYGON")
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 1807, in CreateFeatureclass
raise e
ExecuteError: ERROR 000354: The name contains invalid characters
Failed to execute (CreateFeatureclass).

0 Kudos