Select to view content in your preferred language

phyton script

3216
4
Jump to solution
06-22-2015 02:15 PM
Yeong-SeokJo
Emerging Contributor

Hi there,

I need some help.

For creating polygons on the same latitude, I asked it several days ago.

It's the link.

creating polygons on the same latitude

Darren Wiens gave me a phyton script.

But whenever I tried the script on my arcgis, it was shut down.

I'm wondering about whether it's problem of the script, my shapefile or my arcgis.

Because of my shitty desktop, I tried to reduced the number of copy and my shapefile is so simplified (it's below 5kb).

It's the link

sk.zip - Google Drive

And it's the script I used.

I just replaced Darren's 'polys' with 'sk'.

  1. >>> fc = "sk" 
  2. ... outsk = [] 
  3. ... numCopies = 100 
  4. ... distBtw = 10000 
  5. ... sr = arcpy.Describe(fc).spatialReference 
  6. ... with arcpy.da.SearchCursor(fc,["SHAPE@"],spatial_reference=sr) as cursor: 
  7. ...     for row in cursor: 
  8. ...         for i in range(1,numCopies+1😞 
  9. ...             array = arcpy.Array() 
  10. ...             for part in row[0]: 
  11. ...                 for pnt in part: 
  12. ...                     array.append(arcpy.Point(pnt.X+distBtw*i,pnt.Y)) 
  13. ...             outsk.append(arcpy.Polygon(array,sr)) 
  14. ... arcpy.CopyFeatures_management(outsk,r'in_memory\outsk')
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Make sure your data is projected.

The following Python script will copy features from a layer ("polys"), the number of times specified in "numCopies", separated by the distance "distBtw" (in the layer's linear unit)

^ the script uses the distance unit taken from the spatial reference. Once your coordinate system is defined, it should work.

View solution in original post

4 Replies
DarrenWiens2
MVP Honored Contributor

Make sure your data is projected.

The following Python script will copy features from a layer ("polys"), the number of times specified in "numCopies", separated by the distance "distBtw" (in the layer's linear unit)

^ the script uses the distance unit taken from the spatial reference. Once your coordinate system is defined, it should work.

DanPatterson_Retired
MVP Emeritus

Re: Generating random polygons

for those that want a backgrounder on the premise

0 Kudos
XanderBakker
Esri Esteemed Contributor

Confirmed, the script by Darren Wiens  works with the recommendation that you will need to have a projected coordinate system. Since your data seems to have geographic coordinates I assigned WGS1984 and changed the settings a bit to get a result (dist between of 5 degrees, since the width is a little over 3 degrees and reduced the number of copies to 20):

fc = "sk"
outsk = []
numCopies = 20
distBtw = 5
sr = arcpy.Describe(fc).spatialReference
with arcpy.da.SearchCursor(fc,["SHAPE@"],spatial_reference=sr) as cursor:
    for row in cursor:
        for i in range(1,numCopies+1):
            array = arcpy.Array()
            for part in row[0]:
                for pnt in part:
                    array.append(arcpy.Point(pnt.X+distBtw*i,pnt.Y))
                outsk.append(arcpy.Polygon(array,sr))
    arcpy.CopyFeatures_management(outsk,r'in_memory\outsk')

This yielded the following result:

... which results in the ever lasting question... why would you want this?

And yes I did read the other thread that Dan Patterson refers to..., but the question remains...

Yeong-SeokJo
Emerging Contributor

Thanks.

It worked for me too.

In 1964, there was research about north American mammal distribution by G. G. Simpson.

One of his finding is 'peninsula effect'.

Florida has less mammalian species richness than similar non-peninsula places on the same latitude.

Since he used rough grid system, he wasn't sure about the effect.

Including mammals, most of biodiversity shows latitudinal gradient.

So, I'm comparing Korean peninsula with continental regions. To control size and shape, I wanted to create Korean peninsula polygons inside of Eurasian  and N. American continent.

Then, I'll figure out the specie richness in Korea is weather random event or decided by some factors such as temperature, precipitation, habitat heterogeneity, topology, anthropogenic disturbance and surrounded ocean. 

0 Kudos