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.

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.

- 1 person found this helpful
... and to add to the questions asked by Dan Patterson ...

- may the triangles overlap?
- are there any additional conditions that apply to size and location (located on land size between a and b)?
- and yes... please tell us why you want to do this, what is your objective?

- 3 people found this helpful
... 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()

- 3 people found this helpful
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?

- 1 person found this helpful
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'

- 1 person found this helpful
Hi fumani wada ,

A few pointers:

- When you post cost, please follow the instructions found here: Posting Code blocks in Esri GeoNet . This will save the indents that are required for valid code
- The code you provided will create more than the 75 triangles you mentioned and at this point they are not random.
- I think it is recommendable to ask for specifics, since there are many possible solutions. This way you avoid doing double work
- In the sample code I posted you can see how to create a polygon (triangle) and with the spatial reference, in stead of generating the featureclass and using another tool to define the projection.

The general format is.

- The arcpy.Polygon() takes an array and the spatial reference as parameters
- The arcpy.Array is created from a list
- The list contains the points (arcpy.Point) that define the coordinates repeating the first coordinate as last coordinate.

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...

The post has been removed Xander...the 404 message with This question was

**voluntarily removed by its author.**- 2 people found this helpful
I though I would do another intent by creating some equilateral triangles with a size ranging between 2.5 and 10 degrees:

This is the code used (see code below). For the visualization, I calculated the area in km² using the WGS_1984_Web_Mercator_Auxiliary_Sphere (WKID: 3857) and used that to extrude and symbolize the triangles in ArcGlobe.

import random import arcpy import math def main(): # settings triangles = 75 x_range = (-180.0, 180.0) y_range = (-85.0, 85.0) size_range = (2.5, 10) # decimal degrees sr = arcpy.SpatialReference(4326) # GC_WGS_1984 fc = r"C:\GeoNet\RandomTriangles\test02.shp" feats = [] for i in range(triangles): feat = [] pnt = arcpy.Point(random.uniform(x_range[0], x_range[1]), random.uniform(y_range[0], y_range[1])) polygon = createTriangle(pnt, random.uniform(size_range[0], size_range[1]), sr) polygon = movePolygonInsideExtent(polygon, x_range, y_range) feats.append(polygon) arcpy.CopyFeatures_management(feats, fc) def createTriangle(pnt_cc, size, sr): rotation_range = (0, 120) rotation = random.uniform(rotation_range[0], rotation_range[1]) feat = [] for i in range(3): pnt = createPointAtAngleWithBearing(pnt_cc, rotation + i*120, size) feat.append(pnt) feat.append(feat[0]) return arcpy.Polygon(arcpy.Array(feat), sr) def createPointAtAngleWithBearing(pnt, angle, size): radius = size / math.sqrt(3) # translate side of equilateral triangle to angle = math.radians(angle) dist_x, dist_y = (radius * math.cos(angle), radius * math.sin(angle)) return arcpy.Point(pnt.X + dist_x, pnt.Y + dist_y) def movePolygonInsideExtent(polygon, x_range, y_range): ext = polygon.extent x_move, y_move = 0, 0 if ext.XMin < x_range[0]: x_move = x_range[0] - ext.XMin elif ext.XMax > x_range[1]: x_move = x_range[1] - ext.XMax if ext.YMin < y_range[0]: y_move = y_range[0] - ext.YMin elif ext.YMax > y_range[1]: y_move = y_range[1] - ext.YMax if x_move != 0 or y_move != 0: feat = [] for part in polygon: for pnt in part: pnt = arcpy.Point(pnt.X + x_move, pnt.Y + y_move) feat.append(pnt) return arcpy.Polygon(arcpy.Array(feat), polygon.spatialReference) else: return polygon if __name__ == '__main__': main()

To speed up an answer...