How do I used the random module to create triangles?

4788
11
08-13-2015 04:11 AM
fumaniwada1
New Contributor

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.

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus

To speed up an answer...

  • What code do you have so far?
  • do the triangles have to have equal area?
  • is the orientation to remain fixed?
  • and out of interest...why triangles?
  • which version of python (2.7, 3.4)?
  • for what software?
XanderBakker
Esri Esteemed Contributor

... 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?
XanderBakker
Esri Esteemed Contributor

... 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()
JenniferMcCall4
Occasional Contributor III

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")
fumaniwada1
New Contributor

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?

0 Kudos
DarrenWiens2
MVP Honored Contributor

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.

fumaniwada1
New Contributor

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'

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi fumani wada ,

A few pointers:

  • When you post cost, please follow the instructions found here: Posting Code blocks in the new 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...