Question

Polygon Vertex Order

  • 28 October 2014
  • 6 replies
  • 17 views

Badge
Hello - I am trying to compare polygons I have saved in a database to polygons I am creating using the polygonbuilder. The polygons are basically the same. I am trying to find polygons that have changed shape. I am using the CRCcalulator. I don't really want to use the changedector or matcher. The problem I am having is that some of the polygons are exactly the same except for the order of the vertices which in turn causes different CRC values to be calculated. Is there any way to re-order the polygon vertices so that a polygon's 1st vertex is always in the same place eg. lower left corner. The polygonbuilder seems to randomly pick which vertex will be the 1st vertex.

 

 

6 replies

Badge
I should add that I have trying using the Orientor and it doesn't help.

 

 
Badge +2
Hi,

 

 

Round about solution is to generate points with numbers and unique ID such that you can rebuild the polyon by starting the lower left point (considering near to previous polygon) or else you can take the lower X and lower Y of all the points (some logically calcualtion is required based on the data).

 

 

Hope it helps...

 

 

Pratap
Userlevel 2
Badge +17

 

Hi,

 

 

If you added coordinate values of required first vertex to each feature as attributes (e.g. "x" and "y"), the PythonCaller may be a quick way.

 

-----

 

# Python Script Example

 

# Assume feature geometry is a single-part polygon having a Line boundary;

 

# every feature has attributes "x" and "y" for specifying the first vertex.

 

import fmeobjects

 

 

def rotatePolygonVertices(feature):

 

    # Get coordinate list of original polygon, except end node.

 

    coords = feature.getAllCoordinates()[:-1]

 

    

 

    # Get specified coordinate.

 

    x = float(feature.getAttribute('x'))

 

    y = float(feature.getAttribute('y'))

 

    

 

    # Find index of vertex that matches with the specified coordinate.

 

    # To avoid computational error, find closest vertex from the coordinate.

 

    index = 0

 

    min_d = (coords[0][0] - x)**2 + (coords[0][1] - y)**2

 

    for i, xy in enumerate(coords[1:]):

 

        d = (xy[0] - x)**2 + (xy[1] - y)**2

 

        if d < min_d:

 

            index , min_d = i +1, d

 

            

 

    # Reset polygon if the first vertex doesn't match with the specified coordinate.

 

    # Rotate and close the coordinate list, and then create new polygon.

 

    if 0 < index:

 

        coords = coords[index:] + coords[:index] # rotate

 

        coords.append(coords[0]) # close

 

        newBoundary = fmeobjects.FMELine(coords)

 

        feature.setGeometry(fmeobjects.FMEPolygon(newBoundary))

 

-----

 

 

Takashi
Userlevel 4
Hi,

 

 

what is the reason for not wanting to use the Matcher? It is very efficient and handles large datasets without problem. Takashi's solution is elegant, but I doubt it'd be quicker than the Matcher.

 

 

David
Userlevel 2
Badge +17
Yes, scripting is the second best of course. I too would like to know the reason for not using the Matcher.
Badge +3
I agree with Daid that the Matcher should suffice.

 

You nee to use the option "lenient matching" .

 

That will ignore vertex ordering.

 

 

 

Also you can compare the vertices using only fme transformers, no need to script at all.

Reply