<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: generating random selection of pixels in a raster then store pixel locations/valu in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670912#M51966</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Margaret,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Off the top of my head, creating 10 random points then using the &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//009z0000002t000000"&gt;Extract Values to Points&lt;/A&gt;&lt;SPAN&gt; would work up until the point where 2 points must be in each class 1 thru 5.&amp;nbsp; The only other way I see to incorporate this approach is to create a loop that will create one random point at a time.&amp;nbsp; It will then check the TreeClass value after the Extract Values to Points function.&amp;nbsp; Then a counter variable (one for each class) will monitor which class the random point has.&amp;nbsp; Once the counter reaches 2, the point is not kept, and the loop continues to run and create 1 random point until each counter equals 2.&amp;nbsp; Once 10 points, 2 for each class are created, the files are merged into a feature class.&amp;nbsp; There's no telling how long this will take though.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was able to get most features of Hawth's tools to work in ArcGIS 10 by getting the VBA extension installed as well.&amp;nbsp; If you're using 10.1, you could use &lt;/SPAN&gt;&lt;A href="http://www.spatialecology.com/gme/gmecommands.htm"&gt;GME&lt;/A&gt;&lt;SPAN&gt;.&amp;nbsp; This has incorporated many of the Hawth's functions, but requires R and does not run within ArcGIS.&amp;nbsp; I believe &lt;/SPAN&gt;&lt;A href="http://www.spatialecology.com/gme/rsample.htm"&gt;r.sample&lt;/A&gt;&lt;SPAN&gt; would be the best command to run.&amp;nbsp; This of course still relies on each TreeClass being it's own point feature class.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry I can't be much more of a help.&amp;nbsp; The loop idea would work, but I'm not a python expert so I wouldn't be able to get you any solid sample code to start with.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris B.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 10 Oct 2013 15:22:55 GMT</pubDate>
    <dc:creator>ChristopherBlinn1</dc:creator>
    <dc:date>2013-10-10T15:22:55Z</dc:date>
    <item>
      <title>generating random selection of pixels in a raster then store pixel locations/values</title>
      <link>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670909#M51963</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a large dataset of integer rasters, and was hoping to find a way to generate a "random" selection of pixels for four of the rasters, while making sure all 5 classes, which I've specified based off the pixel values, are equally represented with the selection. Each raster's values ranges from 0-200, with 0-100 representing percent tree coverage and 200 representing water (aka data I want to ignore for this case). I already added a new field to the raster called "TreeClass," and filled that field with numbers 1-6, where 1-5 are different levels/classes of tree coverage and 6 is water/no data values that I want to ignore. In all for each raster, I would like 10 pixels, 2 in each class 1-5.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In addition to randomly selecting these pixels, I would like to record their locations (plus values and classes, if possible), so that I can do a time series analysis on the selected pixels using the other rasters&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any suggestions on the best method for this? I have thought about several approaches: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1. Converting the four rasters to polygon based on TreeClass and then use the Create Random Points tool with the polygon as a constraint. However, after using the Raster to Polygon tool, it doesn't appear to have come out right, and is very slow. Additionally, I don't see a way in which this tool would allow me to store the pixel location (not the coordinates, but samples, lines location data of the pixel).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2. Converting the raster to numpy array and then using python tools to generate random pixels (not yet sure how I would go about this). That would make it easy to store the x, y location of the pixel and then use them to get the same pixel location's value for the other numpy arrays (arrays that I would convert from raster). However, I don't see a way to convert the raster to a structured array so that not only the pixel value would be present and accessible but the class value too. Perhaps I could convert to numpy array that stores the pixel value, somehow add a dimension to the numpy array, and then calculate the class value in python to store into the new dimension.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm hoping there's a simple solution and I'm just over-complicating things, as I tend to do. But I wanted to get some feedback that will help lead me in the right direction.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks so much in advance.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Oct 2013 01:54:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670909#M51963</guid>
      <dc:creator>MargaretWooten</dc:creator>
      <dc:date>2013-10-10T01:54:55Z</dc:date>
    </item>
    <item>
      <title>Re: generating random selection of pixels in a raster then store pixel locations/valu</title>
      <link>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670910#M51964</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Margaret,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are you familiar with Hawth's Analysis Tools?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There is a random selection tool that could help you.&amp;nbsp; You can read on it &lt;/SPAN&gt;&lt;A href="http://www.spatialecology.com/htools/rndsel.php"&gt;here&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To get points, you could convert your rasters to a point feature class using the &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//001200000007000000"&gt;Raster to Point tool&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When you create the points you could add two fields (type double, called lat/lng or X/Y, whichever is more appropriate), and calculate the geometry of each point.&amp;nbsp; This will add the location data to a field.&amp;nbsp; This will give you location and raster value for each point (essentially each pixel in your raster).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From there you could split the layer by attribute value (either manually or with a model), which will create separate point feature classes for each value in your TreeClass field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then you can run the random selection tool on each point feature class for each test.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris B.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Oct 2013 12:28:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670910#M51964</guid>
      <dc:creator>ChristopherBlinn1</dc:creator>
      <dc:date>2013-10-10T12:28:02Z</dc:date>
    </item>
    <item>
      <title>Re: generating random selection of pixels in a raster then store pixel locations/valu</title>
      <link>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670911#M51965</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Chris, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the suggestion! I looked at the Hawth's Analysis dialog and read that it is discontinued/shouldn't be used for later versions of Arc. I was thinking I use your instruction and convert the raster to point shapefile, add the lat/long fields, split the layers based on TreeClass then just use the 'Create Random Point' built in tool in Arc on each tree class shapefile.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, working with these files (which are 4800x4800 pixels) as vectors is proving to be really slow. I successfully ran the Raster to Point on one raster, although it took forever, and splitting these up into 5 shapefiles based on class is taking even longer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Do you have any other ideas that don't require converting to vector? Maybe something with python?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Oct 2013 14:46:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670911#M51965</guid>
      <dc:creator>MargaretWooten</dc:creator>
      <dc:date>2013-10-10T14:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: generating random selection of pixels in a raster then store pixel locations/valu</title>
      <link>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670912#M51966</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Margaret,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Off the top of my head, creating 10 random points then using the &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//009z0000002t000000"&gt;Extract Values to Points&lt;/A&gt;&lt;SPAN&gt; would work up until the point where 2 points must be in each class 1 thru 5.&amp;nbsp; The only other way I see to incorporate this approach is to create a loop that will create one random point at a time.&amp;nbsp; It will then check the TreeClass value after the Extract Values to Points function.&amp;nbsp; Then a counter variable (one for each class) will monitor which class the random point has.&amp;nbsp; Once the counter reaches 2, the point is not kept, and the loop continues to run and create 1 random point until each counter equals 2.&amp;nbsp; Once 10 points, 2 for each class are created, the files are merged into a feature class.&amp;nbsp; There's no telling how long this will take though.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was able to get most features of Hawth's tools to work in ArcGIS 10 by getting the VBA extension installed as well.&amp;nbsp; If you're using 10.1, you could use &lt;/SPAN&gt;&lt;A href="http://www.spatialecology.com/gme/gmecommands.htm"&gt;GME&lt;/A&gt;&lt;SPAN&gt;.&amp;nbsp; This has incorporated many of the Hawth's functions, but requires R and does not run within ArcGIS.&amp;nbsp; I believe &lt;/SPAN&gt;&lt;A href="http://www.spatialecology.com/gme/rsample.htm"&gt;r.sample&lt;/A&gt;&lt;SPAN&gt; would be the best command to run.&amp;nbsp; This of course still relies on each TreeClass being it's own point feature class.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry I can't be much more of a help.&amp;nbsp; The loop idea would work, but I'm not a python expert so I wouldn't be able to get you any solid sample code to start with.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris B.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Oct 2013 15:22:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670912#M51966</guid>
      <dc:creator>ChristopherBlinn1</dc:creator>
      <dc:date>2013-10-10T15:22:55Z</dc:date>
    </item>
    <item>
      <title>Re: generating random selection of pixels in a raster then store pixel locations/valu</title>
      <link>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670913#M51967</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks again for your suggestion! I was able to successfully come up with a method for what I needed using RasterToNumPyArray and then doing everything else in Python. Here is my code, just for reference, and although I'm sure there is a more efficient way to go about this, it at least gave me what I needed:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import numpy
import random

VCtif = "D:\\Maggie Data\\VCF Time Series Data\\Trial\\phase2\\TC_2010.h21v02.tif"
VC = arcpy.RasterToNumPyArray(VCtif) # VC tif will be 1 of 4 VC tiffs, from loop

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# part a) generate random pixels
# create tuples for tree classes
c1 = numpy.where((VC &amp;gt;= 0) &amp;amp; (VC &amp;lt;= 10)) # class 1
c2 = numpy.where((VC &amp;gt;= 11) &amp;amp; (VC &amp;lt;= 20))
c3 = numpy.where((VC &amp;gt;= 21) &amp;amp; (VC &amp;lt;= 40))
c4 = numpy.where((VC &amp;gt;= 41) &amp;amp; (VC &amp;lt;= 60))
c5 = numpy.where((VC &amp;gt;= 61) &amp;amp; (VC &amp;lt;= 100))
classlist = [c1, c2, c3, c4, c5]

points = [] # empty list to store random points (will be a list of tuples)
# loop through c1...c5 and generate 2 random points for each
for c in classlist:
&amp;nbsp;&amp;nbsp;&amp;nbsp; X = c[0].tolist() # x list 
&amp;nbsp;&amp;nbsp;&amp;nbsp; Y = c[1].tolist() # y list

&amp;nbsp;&amp;nbsp;&amp;nbsp; x1= random.choice(X) # choose random point from class n
&amp;nbsp;&amp;nbsp;&amp;nbsp; ind = X.index(x1) # find location of this point
&amp;nbsp;&amp;nbsp;&amp;nbsp; y1 = Y[ind] # find corresponding y point
&amp;nbsp;&amp;nbsp;&amp;nbsp; points.append((x1, y1)) # add to points list

&amp;nbsp;&amp;nbsp;&amp;nbsp; x2= random.choice(X) # do the same for a second point in class n
&amp;nbsp;&amp;nbsp;&amp;nbsp; while x2 == x1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x2 = random.choice(X) # keep generating random second point to ensure it is different from x1
&amp;nbsp;&amp;nbsp;&amp;nbsp; ind2 = X.index(x2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; y2 = Y[ind2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; points.append((x2, y2))

##for i in range(0,10):
##&amp;nbsp;&amp;nbsp;&amp;nbsp; print VC[points&lt;I&gt;] # ensure that values represent 5 class equally&lt;/I&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:18:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generating-random-selection-of-pixels-in-a-raster/m-p/670913#M51967</guid>
      <dc:creator>MargaretWooten</dc:creator>
      <dc:date>2021-12-12T04:18:57Z</dc:date>
    </item>
  </channel>
</rss>

