package esgeomworkshopprofiler;

import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.impl.PackedCoordinateSequenceFactory;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

/**
*
* @author tofarrell
*/
public class BugExample {

    public static void main(String[] args) throws ParseException {

        //Create a geometry backed by a packed coordinate sequence - note that 
the points are in the wrong order
        GeometryFactory factory = new 
GeometryFactory(PackedCoordinateSequenceFactory.DOUBLE_FACTORY);
        WKTReader reader = new WKTReader(factory);
        Geometry geom = reader.read("LINESTRING (100 100, 0 0)");

        // Normalize the geometry - there is a bug here - the normalize method 
assumes that the geometry is backed
        // by a coordinate array. The soft reference to the coordinate array in 
packed coordinate sequence masks
        // this issue
        geom.normalize();

        // Create a clone of the geometry - the clone does not have the changes 
made by normalize
        Geometry clone = (Geometry)geom.clone();

        // The clone is not equal to the original
        if(!clone.toString().equals(geom.toString())){
            throw new IllegalStateException("Clone was not the same as 
original!!!");
        }
    }

    //Proposed fix - replace normalize method in LineString.java

    CoordinateSequence points; // this variable is here only so that the code 
will compile


    public void normalize() {
        for (int i = 0; i < points.size() / 2; i++) {
            int j = points.size() - 1 - i;
            // skip equal points on both ends
            if (!points.getCoordinate(i).equals(points.getCoordinate(j))) {
                if (points.getCoordinate(i).compareTo(points.getCoordinate(j)) 
> 0) {
                    reverse(points);
                }
                return;
            }
        }
    }

    /**
     * Reverse the order of coordinates in a sequence
     * @param sequence
     */
    static void reverse(CoordinateSequence sequence){
        int min = 0;
        int max = sequence.size()-1;
        while(min < max){
            for(int i = sequence.getDimension(); i-- > 0;){
                 double tmp = sequence.getOrdinate(min, i);
                 sequence.setOrdinate(min, i, sequence.getOrdinate(max, i));
                 sequence.setOrdinate(max, i, tmp);
            }
            min++;
            max--;
        }
    }
}
------------------------------------------------------------------------------
_______________________________________________
Jts-topo-suite-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jts-topo-suite-user

Reply via email to