Menu

Creating a polygon from an array of lat/lons

Developers
2022-02-14
2022-02-15
  • Paul Higgins

    Paul Higgins - 2022-02-14

    Been using Jan-Erik's shape cls with my program and they work great. But I often find I have arrays of lat/lons that I want to create a polygon from for example:
    my_points= .array~of("8.919400 118.009485","8.920184 118.008687","8.909866 118.040771","8.901590 118.038006")

    Currently I have to convert the array entries to a string to create the polygon. I want to create the polygon directly from the array. I modified the the polyshape.cls prepare method to do this as follows:

    ::method prepare
    Use Arg polyLine, nofixpoly, xyz, dbug
    If polyLine = 'POLYLINE' Then
       Return .True
    If dbug = .True Then Call Trace '?i'
    self~point = .Array~new
    If polyLine~objectName = 'an Array' Then
    Do
       If 0 < polyLine~Items Then
       Do i = 1 To polyLine~Items
          If polyLine[ i ]~objectName = 'a Point' Then
             self~point[ self~point~Items + 1 ] = .Point~new( polyLine[ i ] )
          Else If polyLine[ i ]~objectName = 'a Line' Then
             self~append( polyLine[ i ] )
          Else do -- NEW
             Parse Value polyLine[ i ] With lat lon
             self~point[ self~point~Items + 1 ] = .Point~new(lon,lat )
          end
       End
    End
    

    So now if I code:

    my_points= .array~of("8.919400 118.009485","8.920184 118.008687","8.909866 118.040771","8.901590 118.038006")
    curr_polyon = .PolyShape~new(my_points)
    say 'curr_polyon='curr_polyon 
    say 'Tostring='curr_polyon~toString(,,.true)
    

    The result is:
    curr_polyon=a Polygon
    Tostring=118.009485,8.9194,0 118.008687,8.920184,0 118.040771,8.909866,0 118.038006,8.90159,0 118.009485,8.9194,0

    Can this be implemented permanently, probably into the WGS84.cls ?

     
    • Ruurd Idenburg

      Ruurd Idenburg - 2022-02-14

      Hi,

      I understand Jan wants this to run on OS2 and
      I am not familiar with the OS2 ooRexx but:

      Why wouldn't you subclass the polyshape class and do something like
      this untested variant instead of asking your stuff to be incorporated:


      ::class myPolyShape public subclass polyShape

      ::method prepare
      Use Arg polyLine, nofixpoly, xyz, dbug
      If polyLine = 'POLYLINE' Then
         Return .True
      If dbug = .True Then Call Trace '?i'
      self~point = .Array~new
      If polyLine~objectName = 'an Array' Then
      -- I would use polyLine~class~id = 'Array' because objectName is mutable
      Do
      -- This If superfluous
         If 0 < polyLine~Items Then
         Do i = 1 To polyLine~Items
            objName = polyLine[ i ]~objectName
      -- again replace objectName with class~id and 'Point' and 'Line'
            If (objName='a Point' | objName='a Line') Then
                prepare:super(polyLine, nofixpoly, xyz, dbug)
            Else Do -- NEW
               Parse Value polyLine[ i ] With lat lon
      -- What's wrong with self~point~append(.Point~new(lot, lan)
               self~point[ self~point~Items + 1 ] = .Point~new(lon,lat )
            End
         End
      End


      Hope this helps in some way

      Ruurd

      On 2/14/22 19:46, Paul Higgins wrote:

      Been using Jan-Erik's shape cls with my program and they work great.
      But I often find I have arrays of lat/lons that I want to create a
      polygon from for example:
      |my_points= .array~of("8.919400 118.009485","8.920184
      118.008687","8.909866 118.040771","8.901590 118.038006")|

      Currently I have to convert the array entries to a string to create
      the polygon. I want to create the polygon directly from the array. I
      modified the the polyshape.cls prepare method to do this as follows:

      |::method prepare Use Arg polyLine, nofixpoly, xyz, dbug If polyLine =
      'POLYLINE' Then Return .True If dbug = .True Then Call Trace '?i'
      self~point = .Array~new If polyLine~objectName = 'an Array' Then Do If
      0 < polyLine~Items Then Do i = 1 To polyLine~Items If polyLine[ i
      ]
      ~objectName = 'a Point' Then self~point[ self~point~Items + 1 ] =
      .Point~new( polyLine[ i ] ) Else If polyLine[ i ]~objectName = 'a
      Line' Then self~append( polyLine[ i ] ) Else do -- NEW Parse Value
      polyLine[ i ] With lat lon self~point[ self~point~Items + 1 ] =
      .Point~new(lon,lat ) end End End |

      So now if I code:

      |my_points= .array~of("8.919400 118.009485","8.920184
      118.008687","8.909866 118.040771","8.901590 118.038006") curr_polyon =
      .PolyShape~new(my_points) say 'curr_polyon='curr_polyon say
      'Tostring='curr_polyon~toString(,,.true) |

      The result is:
      curr_polyon=a Polygon
      Tostring=118.009485,8.9194,0 118.008687,8.920184,0
      118.040771,8.909866,0 118.038006,8.90159,0 118.009485,8.9194,0

      Can this be implemented permanently, probably into the WGS84.cls ?


      Creating a polygon from an array of lat/lons
      https://sourceforge.net/p/oorexx/discussion/408479/thread/87f4a7697e/?limit=25#f9c9


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/oorexx/discussion/408479/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
  • Paul Higgins

    Paul Higgins - 2022-02-15

    OK thanks Ruurd

     
  • Paul Higgins

    Paul Higgins - 2022-02-15

    Or I could do it like this:
    :~~~
    :class myPolyShape public subclass polyShape

    ::method init
    Use Arg polyLine, nofixpoly, xyz, dbug
    points = .Array~new
    Do i = 1 To polyLine~Items
    Parse Value polyLine[ i ] With lat lon
    points~append(.Point~new(lon,lat))
    end
    self~init:super( points, nofixpoly, xyz, dbug )
    ~~~
    It works. Maybe better than overriding the prepare method

     

Log in to post a comment.