I have a shapefile of the world and I am trying to create 75 random triangles over this shapefile using the random package. I'm just not sure how to do this and use a for loop to create the lat and.
To speed up an answer...
... and to add to the questions asked by Dan Patterson ...
... and to visualize the reason for the questions:
Personally, I don't think you are interested in something like this.
Example code:
def main(): import random import arcpy # settings x_range = (-180.0, 180.0) y_range = (-90.0, 90.0) triangles = 75 sr = arcpy.SpatialReference(4326) # GC_WGS_1984 fc = r"D:\Xander\GeoNet\RandomTriangles\test01.shp" feats = [] for i in range(triangles): feat = [] for j in range(3): x = random.uniform(x_range[0], x_range[1]) y = random.uniform(y_range[0], y_range[1]) pnt = arcpy.Point(x,y) feat.append(pnt) feat.append(feat[0]) polygon = arcpy.Polygon(arcpy.Array(feat), sr) feats.append(polygon) arcpy.CopyFeatures_management(feats, fc) if __name__ == '__main__': main()
You could try something like this. It requires you to set the output spatial reference, range of lat/long values, and the output feature class location to what you're using.
import arcpy, random # Create list to hold groupings of coordinates triangleCoordList = [] # Create list to hold all polygon objects triangleList = [] # Create spatial reference parameter sr = arcpy.Describe("C:\Users\jmccall\Desktop\Test.gdb\TriangleSRSample").spatialReference # Set triangle counter countTriangles = 0 # Start loop for 75 triangles while countTriangles < 75: ## Create 3 pairs of lat/long values # Set coordinate counter countCoords = 0 # Create list to hold coordinates for single triangle coordList = [] while countCoords < 3: latLongList = [] # Cerate list # Create lat value lat = random.uniform(50.994219776,51.066472899) # Create long number long = random.uniform(-113.772229212,-113.865563728) latLongList.append(long) latLongList.append(lat) # Update lat long coordinate list coordList.append(latLongList) # Update coordinate counter countCoords = countCoords + 1 # Append coordinate list to complete list of coordinates triangleCoordList.append(coordList) # Update triangle counter countTriangles = countTriangles + 1 ## Create features # Loop through coordinate pairs in list for feature in triangleCoordList: triangleList.append(arcpy.Polygon(arcpy.Array([arcpy.Point(*coords) for coords in feature]),sr)) # Copy polygon objects to the Triangle feature class arcpy.CopyFeatures_management(triangleList, "C:\Users\jmccall\Desktop\Test.gdb\Triangle")
I tried this code and it ran in arcpy but in arcmap no triangles showed up I'm guessing thats because in line 48 you put arcpy.Point(*coords) which has no particular coordinate? Did you use a random set of coordinates or did the triangles show up on your map?
See here for help on unpacking arguments. Basically, *coords is Python shorthand for "the list held in the coords variable", which in this case is the list of coordinate pairs for each point (feature), from the variable triangleCoordList.
I came up with this code although it is buggy. I think that the triangles are supposed to be the same size. The triangles are just supposed to be random using the import random module I wasn't given specifics about the size. She just said she needs 75 of them. Based on the example problem I configured this code.
I'm not sure how to configure the loop so I can set the points/coordinates for a triangle using an array
import arcpy
import random
env = the path lol
feat = 'triangles.shp'
polygon_list = []
#I will create random triangles across the world shapefile using the random module
for y in range (90,0,-10):
for x in range(-180,180,10):
cornersArray = arcpy.Array()
p1 = arcpy.Point()
p1.X = x
p1.Y = y
cornersArray.add(p1)
p2 = arcpy.Point()
p2.X = x + 10
p2.Y = y
cornersArray.add(p2)
p3 = arcpy.Point()
p3.X=x+10
p3.Y = y-10
cornersArray.add(p3)
poly = arcpy.Polygon(cornersArray)
polygon_list.append(poly)
#I will copy the features in my list to the triangle shapefile
arcpy.CopyFeatures_management(polygon_list,feat)
#I will define the projection for the triangles shapefile
arcpy.DefineProjection_management(feat,arcpy.SpatialReference(4326))
print 'Finished created the triangles'
Hi fumani wada ,
A few pointers:
The general format is.
It is possible to make a tool that let's you play with sizes, minimum and maximum angles of the triangle, rotation, and much more. The most important thing is to know what the person that asked for it expects...