Skip to content

Commit

Permalink
Factorisation of some operations in GIS files.
Browse files Browse the repository at this point in the history
Addition of the "dimension" to CoordinateSequences to comply with
GeoTools and JTS.
  • Loading branch information
AlexisDrogoul committed May 4, 2021
1 parent 59dd46b commit f98459f
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 493 deletions.
Expand Up @@ -14,14 +14,13 @@
import org.geotools.filter.text.ecql.ECQL;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.jdbc.JDBCDataStoreFactory;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.Feature;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryType;
import org.opengis.filter.Filter;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import org.locationtech.jts.geom.Geometry;

import msi.gama.common.geometry.Envelope3D;
import msi.gama.common.util.FileUtils;
import msi.gama.metamodel.shape.GamaGisGeometry;
Expand Down Expand Up @@ -142,7 +141,7 @@ public DataStore Connect(final IScope scope) throws Exception {
dStore = DataStoreFinder.getDataStore(connectionParameters); // get
// connection
// DEBUG.LOG("data store postgress:" + dStore);
if (dStore == null) { throw new IOException("Can't connect to " + database); }
if (dStore == null) throw new IOException("Can't connect to " + database);
return dStore;
}

Expand All @@ -152,9 +151,8 @@ public DataStore Connect(final IScope scope) throws Exception {
public void close(final IScope scope) throws GamaRuntimeException {
if (dataStore != null) {
dataStore.dispose();
} else {
} else
throw GamaRuntimeException.error("The connection to " + this.database + " is not opened ", scope);
}
}

public void setDataStore(final DataStore dataStore) {
Expand Down Expand Up @@ -208,7 +206,7 @@ private void readTable(final IScope scope, final String tableName, final String
*/
@Override
protected void fillBuffer(final IScope scope) throws GamaRuntimeException {
if (getBuffer() != null) { return; }
if (getBuffer() != null) return;
setBuffer(GamaListFactory.<IShape> create(Types.GEOMETRY));
readTable(scope);
}
Expand Down Expand Up @@ -291,4 +289,9 @@ public SimpleFeatureCollection getRecordSet() {
}
}// end of class QueryInfo

@Override
protected SimpleFeatureCollection getFeatureCollection(final IScope scope) {
return null;
}

}
Expand Up @@ -11,14 +11,13 @@
package msi.gama.common.geometry;

import static com.google.common.collect.Iterators.forArray;
import static org.locationtech.jts.algorithm.CGAlgorithms.signedArea;
import static msi.gama.common.geometry.GamaGeometryFactory.isRing;
import static org.locationtech.jts.algorithm.CGAlgorithms.signedArea;

import java.util.Arrays;
import java.util.Iterator;

import org.apache.commons.lang.ArrayUtils;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;

Expand All @@ -35,6 +34,8 @@

public class GamaCoordinateSequence implements ICoordinates {

final int dimension;

/**
* The final array of GamaPoint, considered to be internally mutable (i.e. points can be changed inside)
*/
Expand All @@ -48,8 +49,8 @@ public class GamaCoordinateSequence implements ICoordinates {
* @param points2
* an array of points
*/
GamaCoordinateSequence(final Coordinate... points2) {
this(true, points2);
GamaCoordinateSequence(final int dimension, final Coordinate... points2) {
this(dimension, true, points2);
}

/**
Expand All @@ -62,7 +63,8 @@ public class GamaCoordinateSequence implements ICoordinates {
* @param points2
* an array of points
*/
GamaCoordinateSequence(final boolean copy, final Coordinate... points2) {
GamaCoordinateSequence(final int dimension, final boolean copy, final Coordinate... points2) {
this.dimension = dimension;
if (copy) {
final int size = points2.length;
points = new GamaPoint[size];
Expand All @@ -81,7 +83,8 @@ public class GamaCoordinateSequence implements ICoordinates {
* @param size
* an int > 0 (negative sizes will be treated as 0)
*/
GamaCoordinateSequence(final int size) {
GamaCoordinateSequence(final int dimension, final int size) {
this.dimension = dimension;
points = new GamaPoint[size < 0 ? 0 : size];
for (int i = 0; i < size; i++) {
points[i] = new GamaPoint(0d, 0d, 0d);
Expand All @@ -95,17 +98,18 @@ public class GamaCoordinateSequence implements ICoordinates {
*/
@Override
public int getDimension() {
return 3;
return dimension;
}

/**
* Makes a complete copy of this sequence (incl. cloning the points themselves)
*/
@Override
public final GamaCoordinateSequence copy() {
return new GamaCoordinateSequence(true, points);
return new GamaCoordinateSequence(dimension, true, points);
}


@Override
@Deprecated
public GamaCoordinateSequence clone() {
return copy();
Expand Down Expand Up @@ -257,7 +261,7 @@ public ICoordinates yNegated() {
// CW property is ensured by reversing the resulting array
points2[i] = points[size - i - 1].yNegated();
}
return new GamaCoordinateSequence(false, points2);
return new GamaCoordinateSequence(dimension, false, points2);
}

@Override
Expand Down Expand Up @@ -314,7 +318,7 @@ public void visit(final PairVisitor v) {
@Override
public void getNormal(final boolean clockwise, final double factor, final GamaPoint normal) {
normal.setLocation(0, 0, 0);
if (points.length < 3) { return; }
if (points.length < 3) return;
for (int i = 0; i < points.length - 1; i++) {
final GamaPoint v0 = points[i];
final GamaPoint v1 = points[i + 1];
Expand Down Expand Up @@ -343,7 +347,7 @@ public Envelope3D getEnvelopeInto(final Envelope3D envelope) {
@Override
public double averageZ() {
double sum = 0d;
if (points.length == 0) { return sum; }
if (points.length == 0) return sum;
for (final GamaPoint p : points) {
sum += p.z;
}
Expand Down Expand Up @@ -395,7 +399,7 @@ public void applyRotation(final Rotation3D rotation) {

@Override
public void replaceWith(final int i, final double x, final double y, final double z) {
if (i < 0 || i >= points.length) { return; }
if (i < 0 || i >= points.length) return;
points[i].setLocation(x, y, z);

}
Expand All @@ -404,7 +408,7 @@ public void replaceWith(final int i, final double x, final double y, final doubl
public boolean isHorizontal() {
final double z = points[0].z;
for (int i = 1; i < points.length; i++) {
if (points[i].z != z) { return false; }
if (points[i].z != z) return false;
}
return true;
}
Expand All @@ -429,7 +433,7 @@ public void setAllZ(final double elevation) {
@Override
public boolean isCoveredBy(final Envelope3D env) {
for (final GamaPoint point : points) {
if (!env.covers(point)) { return false; }
if (!env.covers(point)) return false;
}
return true;
}
Expand Down Expand Up @@ -484,15 +488,13 @@ public void translateBy(final double x, final double y, final double z) {
*/
@Override
public void ensureClockwiseness() {
if (!isRing(points)) { return; }
if (signedArea(points) <= 0) {
ArrayUtils.reverse(points);
}
if (!isRing(points)) return;
if (signedArea(points) <= 0) { ArrayUtils.reverse(points); }
}

@Override
public boolean equals(final Object object) {
if (!(object instanceof GamaCoordinateSequence)) { return false; }
if (!(object instanceof GamaCoordinateSequence)) return false;
final GamaCoordinateSequence other = (GamaCoordinateSequence) object;
return Arrays.equals(points, other.points);
}
Expand Down
Expand Up @@ -6,7 +6,7 @@
* (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package msi.gama.common.geometry;

Expand All @@ -20,41 +20,41 @@ public class GamaCoordinateSequenceFactory implements CoordinateSequenceFactory

/**
* Method create()
*
*
* @see org.locationtech.jts.geom.CoordinateSequenceFactory#create(org.locationtech.jts.geom.Coordinate[])
*/
@Override
public ICoordinates create(final Coordinate[] coordinates) {
if (coordinates.length == 1) { return new UniqueCoordinateSequence(coordinates[0]); }
return new GamaCoordinateSequence(coordinates);
if (coordinates.length == 1) return new UniqueCoordinateSequence(3, coordinates[0]);
return new GamaCoordinateSequence(3, coordinates);
}

public ICoordinates create(final GamaPoint[] coordinates, final boolean copy) {
if (coordinates.length == 1) { return new UniqueCoordinateSequence(coordinates[0]); }
return new GamaCoordinateSequence(copy, coordinates);
if (coordinates.length == 1) return new UniqueCoordinateSequence(3, coordinates[0]);
return new GamaCoordinateSequence(3, copy, coordinates);
}

/**
* Method create()
*
*
* @see org.locationtech.jts.geom.CoordinateSequenceFactory#create(org.locationtech.jts.geom.CoordinateSequence)
*/
@Override
public ICoordinates create(final CoordinateSequence coordSeq) {
if (coordSeq.size() == 1) { return new UniqueCoordinateSequence(coordSeq.getCoordinate(0)); }
if (coordSeq instanceof GamaCoordinateSequence) { return ((GamaCoordinateSequence) coordSeq).clone(); }
return new GamaCoordinateSequence(coordSeq.toCoordinateArray());
public ICoordinates create(final CoordinateSequence cs) {
if (cs.size() == 1) return new UniqueCoordinateSequence(cs.getDimension(), cs.getCoordinate(0));
if (cs instanceof GamaCoordinateSequence) return ((GamaCoordinateSequence) cs).clone();
return new GamaCoordinateSequence(cs.getDimension(), cs.toCoordinateArray());
}

/**
* Method create()
*
*
* @see org.locationtech.jts.geom.CoordinateSequenceFactory#create(int, int)
*/
@Override
public ICoordinates create(final int size, final int dimension) {
if (size == 1) { return new UniqueCoordinateSequence(new GamaPoint()); }
return new GamaCoordinateSequence(size);
if (size == 1) return new UniqueCoordinateSequence(dimension, new GamaPoint());
return new GamaCoordinateSequence(dimension, size);
}

}
11 changes: 6 additions & 5 deletions msi.gama.core/src/msi/gama/common/geometry/ICoordinates.java
Expand Up @@ -45,7 +45,7 @@ public interface VertexVisitor {
/**
* The empty coordinate sequence
*/
ICoordinates EMPTY = new GamaCoordinateSequence(new GamaPoint[] {});
ICoordinates EMPTY = new GamaCoordinateSequence(3, new GamaPoint[] {});

/**
* Returns the geometric center of this sequence of points
Expand All @@ -62,9 +62,10 @@ default void getCenter(final GamaPoint center) {
center.setLocation(0, 0, 0);
addCenterTo(center);
}


@Override
@Deprecated
default CoordinateSequence clone() {
default CoordinateSequence clone() {
return copy();
}

Expand All @@ -86,7 +87,7 @@ default CoordinateSequence clone() {
* @return a point or null
*/
default GamaPoint at(final int i) {
if (i > size() || i < 0) { return null; }
if (i > size() || i < 0) return null;
return getCoordinate(i);
}

Expand Down Expand Up @@ -175,7 +176,7 @@ default GamaPoint getNormal(final boolean clockwise) {
final GamaPoint normal = new GamaPoint();
getNormal(clockwise, 1, normal);
return normal;
};
}

/**
* Computes the normal to this sequence, multiplying the resulting unit vector by a given factor, and fills the
Expand Down

0 comments on commit f98459f

Please sign in to comment.