Skip to content

Commit

Permalink
Add 'cells_overlapping' to provide better results than the cells_in
Browse files Browse the repository at this point in the history
when geometry are polylines.
  • Loading branch information
benoitgaudou committed Mar 15, 2022
1 parent 892a4c4 commit 80fd605
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
19 changes: 18 additions & 1 deletion msi.gama.core/src/msi/gama/util/matrix/GamaField.java
Expand Up @@ -341,9 +341,26 @@ public IList<IShape> getCellsIntersecting(final IScope scope, final IShape shape
}
}
return inEnv;

}

@Override
public IList<IShape> getCellsOverlapping(final IScope scope, final IShape shape) {
computeDimensions(scope);
Envelope3D env = Envelope3D.of(shape);
IList<IShape> inEnv = GamaListFactory.create(Types.GEOMETRY);
GamaPoint p = new GamaPoint();
for (double i = env.getMinX(); i < env.getMaxX(); i += cellDimensions.x) {
for (double j = env.getMinY(); j < env.getMaxY(); j += cellDimensions.y) {
p.setLocation(i, j, 0);
IShape s = getCellShapeAt(scope, p);
if( (s!=null) && (s.intersects(shape)) ){
inEnv.add(s);
}
}
}
return inEnv;
}

@Override
public IList<GamaPoint> getLocationsIntersecting(final IScope scope, final IShape shape) {
computeDimensions(scope);
Expand Down
10 changes: 10 additions & 0 deletions msi.gama.core/src/msi/gama/util/matrix/IField.java
Expand Up @@ -177,6 +177,16 @@ default void setCellSize(final IScope scope, final GamaPoint size) {
*/
IList<IShape> getCellsIntersecting(IScope scope, IShape shape);

/**
* Returns a list of the 'cells' (rectangle shapes) that overlap the geometry passed in parameter
*
* @param scope
* @param shape
* @return
*/
IList<IShape> getCellsOverlapping(IScope scope, IShape shape);


/**
* Gets the locations intersecting.
*
Expand Down
27 changes: 25 additions & 2 deletions msi.gama.core/src/msi/gaml/types/GamaFieldType.java
Expand Up @@ -271,7 +271,7 @@ public static IShape buildShapeFromFieldLocation(final IScope scope, final IFiel
}

/**
* Gets the shapes from geometry.
* Gets the shapes from geometry (cells with a point inside the geometry).
*
* @param scope the scope
* @param field the field
Expand All @@ -284,10 +284,33 @@ public static IShape buildShapeFromFieldLocation(final IScope scope, final IFiel
content_type = IType.GEOMETRY,
category = { IOperatorCategory.GRID },
concept = { IConcept.GRID },
doc = { @doc ("Returns the list of 'cells' that 'intersect' with the geometry passed in argument. The cells are ordered by their x-, then y-coordinates") })
doc = { @doc ("Returns the list of 'cells' that 'intersect' with the geometry passed in argument. "
+ "(Intersection is understood as the cell center is insside the geometry; if the geometry is a polyline or a point, results will not be accurate."
+ "The cells are ordered by their x-, then y-coordinates") })
public static IList<IShape> getShapesFromGeometry(final IScope scope, final IField field, final IShape shape) {
return field.getCellsIntersecting(scope, shape);
}

/**
* Gets the shapes from geometry (cells overlapping the geometry).
*
* @param scope the scope
* @param field the field
* @param shape the shape
* @return the shapes from geometry
*/
@operator (
value = "cells_overlapping",
can_be_const = false,
content_type = IType.GEOMETRY,
category = { IOperatorCategory.GRID },
concept = { IConcept.GRID },
doc = { @doc ("Returns the list of 'cells' that 'overlap' the geometry passed in argument. "
+ "It is much less efficient than the cells_in operator, but is relevant is a polynie or a point. "
+ "The cells are ordered by their x-, then y-coordinates") })
public static IList<IShape> getShapesOverGeometry(final IScope scope, final IField field, final IShape shape) {
return field.getCellsOverlapping(scope, shape);
}

/**
* Gets the values from geometry.
Expand Down

0 comments on commit 80fd605

Please sign in to comment.