I have a number of point objects (n) with various coordinates. I have an agent with coordinates.
I want to find all points within a certain distance of point a and put them into a list.
public class Agent {
private Context<Object> context;
private Geography<Object> geography;
...
public Agent(Context<Object> context, Geography<Object> geography) {
this.context = context;
this.geography = geography;
}
...
public void step() {
//gets the coordinates of the agent, calls them coord
Context context = ContextUtils.getContext(this);
Geography<Agent> geography = (Geography)context.getProjection("Geography");
Geometry geom = geography.getGeometry(this);
Coordinate coord = geom.getCoordinates()[0];
//creates a list of called points
List<Object> points = new ArrayList<Object>();
//creates an envelope object and creates envelope dimensions
Envelope envelope = new Envelope((coord.x + 0.0001, coord.y + 0.0001, coord.x - 0.001, coord.y - 0.001);
//for all objects within the envelope, if they are of the specific class type, add them to the list
for(Object obj: geography.getObjectsWithin(envelope, Specific.class)) {
trees.add(tree);
}
System.out.println("The number of objects in the envelope is : " + trees.size());
My question is: when I use these dimensions (in the code above), I get 1342 objects in my envelope. This is presumably a very small envelope that should encompass at the most 200-300. Why is it making one so large?
It is possible I am not correctly creating the envelope. Does anyone know more about the details of how to specify these envelope dimensions?
You instanciate your Envelope
wrong, it is not Envelope(minX, minY, maxX, maxY)
but Envelope(minX, maxX, minY, maxY)
.
And in the constructor of Envelope
, it automatically check if minX
is really the minimum betwwen minX
and maxX
otherwise it will swap minX
and maxX
. The constructor is in fact Envelope(x1, x2, y1, y2)
Take a look at the Envelope
documentation here