[Baypiggies] Hi all -can anyone convert this code to python---many thanks

Chris Calloway cbc at unc.edu
Thu Dec 14 21:04:10 CET 2006


Sharon Kazemi wrote:
> Hi all,
> 
> have below code that needs to be recoded into Python for use in ESRI ArcGIS
> software.
> I would greatly appreciate any assistance.
> Thanks
> Sheri

Chill. There are lots of people who have to do GIS who are not yet 
Python proficient. ESRI threw its users off the Python deep end on in 
the last couple of years. Most of them are not even programmers. "Model 
builders" generate code for them. And Ms. Kazemi wants user contributed 
code ported. There is a huge base of free user contributed code which 
has not yet been ported. The code in the ESRI store, which sells for 
thousands, does not come with source.

Here it is recoded in Python. I suspect many people could do this 
blindfolded, as there was nothing which wasn't straightforward in the 
code. For use in ArcGIS, it depends what in ArcGIS is calling it. The 
original C function is being passed pointers to C arrays in memory, and 
expecting both a return value (index of the intermediate node) and a 
memory location pointed to set (to the square of the distance). This 
Python code does the same Pythonically, expecting tuples and returning a 
tuple. But you will have to call it Pythonically. Python invokes ArcGIS 
code through a win32com object. What is calling Ms. Kazemi's code is 
unknown. The computation of distMaxSquare looks pretty squirrelly as well.

And thanks for reminding me once again why I do Python. Just looking at 
C anymore gives me a headache.

The following is untested and unwarranted:

def furthestFromSegment(startindex, endindex, x, y):
     """Inner Loop of the Douglas-Peucker line generalization algorithm.

     furthestFromSegment(long, long, tuple, tuple) -> (long, float)
     """

     distMaxSquare = -1.0
     if endindex < (startindex + 2):
         return (startindex, distMaxSquare)

     outindex = startindex
     bx = x[startindex]
     by = y[startindex]
     cx = x[endindex]
     cy = y[endindex]
     bcx = cx - bx
     bcy = cy - by
     bcSquare = (bcx**2) + (bcy**2)

     for index in range(startindex + 1, endindex):
         ax = x[index]
         ay = y[index]
         bax = ax - bx
         bay = ay - by

         if ((bcx * bax) + (bcy * bay)) <= 0.0:
             distSquare = (bax**2) + (bay**2)
         else:
             cax = ax - cx
             cay = ay - cy

             if ((bcx * cax) + (bcy * cay)) >= 0.0:
                 distSquare = (cax**2) + (cay**2)
             else:
                 distSquare = (cax * bay) - (cay * bax)
                 distSquare = (distSquare**2) / bcSquare

         if distSquare > distMaxSquare:
             outindex = index
             distMaxSquare = distSquare

     return (outindex, distMaxSquare)

-- 
Sincerely,

Chris Calloway
http://www.seacoos.org
office: 332 Chapman Hall   phone: (919) 962-4323
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599


More information about the Baypiggies mailing list