Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.locationtech.jts.geom.impl;

import java.io.Serializable;
import java.lang.reflect.Array;

import org.locationtech.jts.geom.*;

Expand Down Expand Up @@ -82,13 +83,14 @@ public CoordinateArraySequence(Coordinate[] coordinates, int dimension) {
*/
public CoordinateArraySequence(Coordinate[] coordinates, int dimension, int measures)
{
this.coordinates = coordinates;
this.dimension = dimension;
this.measures = measures;
if (coordinates == null) {
this.coordinates = new Coordinate[0];
}
enforceArrayConsistency( this.coordinates );
else {
this.coordinates = enforceArrayConsistency(coordinates);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This provides the "make a copy if needed"

}
}

/**
Expand Down Expand Up @@ -158,23 +160,42 @@ public CoordinateArraySequence(CoordinateSequence coordSeq)

/**
* Ensure array contents of the same type, making use of {@link #createCoordinate()} as needed.
* <p>
* A new array will be created if needed to return a consistent result.
* </p>
*
* @param array array is modified in place as needed
* @param array array containing consistent coordinate instances
*/
protected void enforceArrayConsistency(Coordinate[] array)
protected Coordinate[] enforceArrayConsistency(Coordinate[] array)
{
Coordinate sample = createCoordinate();
Class<?> type = sample.getClass();
boolean isConsistent=true;
for( int i = 0; i < array.length; i++) {
Coordinate coordinate = array[i];
if( coordinate == null ) {
array[i] = createCoordinate();
if( coordinate != null && !coordinate.getClass().equals(type)) {
isConsistent = false;
break;
}
else if(!coordinate.getClass().equals(type)) {
Coordinate duplicate = createCoordinate();
duplicate.setCoordinate(coordinate);
array[i] = duplicate;
}
if( isConsistent ){
return array;
}
else {
Class<? extends Coordinate> coordinateType = sample.getClass();
Coordinate copy[] = (Coordinate[]) Array.newInstance(coordinateType, array.length);
for ( int i = 0; i < copy.length; i++){
Coordinate coordinate = array[i];
if( coordinate != null && !coordinate.getClass().equals(type)){
Coordinate duplicate = createCoordinate();
duplicate.setCoordinate(coordinate);
copy[i] = duplicate;
}
else {
copy[i] = coordinate;
}
}
return copy;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ public void testMixedCoordinates()
Coordinate coord1 = new Coordinate(1.0,1.0,1.0);
CoordinateXY coord2 = new CoordinateXY(2.0,2.0);
CoordinateXYM coord3 = new CoordinateXYM(3.0,3.0,3.0);
Coordinate[] array = new Coordinate[] {coord1, coord2, coord3};

Coordinate[] array = new Coordinate[] {coord1, coord2, coord3, null};
CoordinateSequence seq = factory.create(array);
assertEquals( 3, seq.getDimension());
assertEquals( 1, seq.getMeasures());
assertTrue( coord1.equals( seq.getCoordinate(0)));
assertTrue( coord2.equals( seq.getCoordinate(1)));
assertTrue( coord3.equals( seq.getCoordinate(2)));
assertNull( seq.getCoordinate(3));
}

private void initProgression(CoordinateSequence seq) {
Expand Down