Skip to content

Commit

Permalink
Replaces JSI in favor of JTS Topology Suite (#226)
Browse files Browse the repository at this point in the history
Closes #224
  • Loading branch information
jazzido authored May 13, 2018
1 parent e37c666 commit 0705bc6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 88 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@

<dependencies>
<dependency>
<groupId>net.sf.jsi</groupId>
<artifactId>jsi</artifactId>
<version>1.1.0-SNAPSHOT</version>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>1.15.0</version>
</dependency>

<dependency>
Expand Down
69 changes: 15 additions & 54 deletions src/main/java/technology/tabula/RectangleSpatialIndex.java
Original file line number Diff line number Diff line change
@@ -1,87 +1,48 @@
package technology.tabula;

import gnu.trove.procedure.TIntProcedure;

import java.util.ArrayList;
import java.util.List;

import net.sf.jsi.SpatialIndex;
import net.sf.jsi.rtree.RTree;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.index.strtree.STRtree;

class RectangleSpatialIndex<T extends Rectangle> {

class SaveToListProcedure implements TIntProcedure {
private List<Integer> ids = new ArrayList<>();

@Override public boolean execute(int id) {
ids.add(id);
return true;
}

List<Integer> getIds() {
return ids;
}
}
private final STRtree si = new STRtree();
private final List<T> rectangles = new ArrayList<>();

private final SpatialIndex si;
private final List<T> rectangles;
private Rectangle bounds = null;

public RectangleSpatialIndex() {
si = new RTree();
si.init(null);
rectangles = new ArrayList<>();
}

public void add(T te) {
rectangles.add(te);
if (bounds == null) {
bounds = new Rectangle();
bounds.setRect(te);
}
else {
bounds.merge(te);
}
si.add(rectangleToSpatialIndexRectangle(te), rectangles.size() - 1);
si.insert(new Envelope(te.getLeft(), te.getRight(), te.getBottom(), te.getTop()), te);
}

public List<T> contains(Rectangle r) {
SaveToListProcedure proc = new SaveToListProcedure();
si.contains(rectangleToSpatialIndexRectangle(r), proc);
ArrayList<T> rv = new ArrayList<>();
for (int i : proc.getIds()) {
rv.add(rectangles.get(i));
List<T> intersection = si.query(new Envelope(r.getLeft(), r.getRight(), r.getTop(), r.getBottom()));
List<T> rv = new ArrayList<T>();

for (T ir: intersection) {
if (r.contains(ir)) {
rv.add(ir);
}
}

Utils.sort(rv, Rectangle.ILL_DEFINED_ORDER);
return rv;
}

public List<T> intersects(Rectangle r) {
SaveToListProcedure proc = new SaveToListProcedure();
si.intersects(rectangleToSpatialIndexRectangle(r), proc);
ArrayList<T> rv = new ArrayList<>();
for (int i : proc.getIds()) {
rv.add(rectangles.get(i));
}
Utils.sort(rv, Rectangle.ILL_DEFINED_ORDER);
List rv = si.query(new Envelope(r.getLeft(), r.getRight(), r.getTop(), r.getBottom()));
return rv;
}

private net.sf.jsi.Rectangle rectangleToSpatialIndexRectangle(Rectangle r) {
return new net.sf.jsi.Rectangle((float) r.getX(),
(float) r.getY(),
(float) (r.getX() + r.getWidth()),
(float) (r.getY() + r.getHeight()));
}


/**
* Minimum bounding box of all the Rectangles contained on this RectangleSpatialIndex
*
* @return a Rectangle
*/
public Rectangle getBounds() {
return bounds;
return Rectangle.boundingBoxOf(rectangles);
}

}
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
"",HARVEST,VARIATION,,,
"","11/12
(a)",12/13,Percentage,Absolute,
"",,"May/2013
(b)","Jun/2013
(c)",(c/a),(c-a)
COTTON,"1.393,4","886,7","894,9","(35,8)","( 498,5)"
TOTAL PEANUT,"93,9","100,6","100,2","6,7","6,3"
PEANUT 1ST CROP,"82,1","86,3","86,2","5,0","4,1"
PEANUT 2ND CROP,"11,8","14,3","14,0","18,6","2,2"
RICE,"2.426,7","2.389,7","2.396,0","(1,3)","( 30,7)"
TOTAL BEANS,"3.262,1","2.952,7","3.026,9","(7,2)","( 235,2)"
BEANS 1ST CROP,"1.241,4","1.122,6","1.122,9","(9,5)","( 118,5)"
BEANS 2ND CROP,"1.394,6","1.275,4","1.271,7","(8,8)","( 122,9)"
BEANS 3RD CROP,"626,1","554,7","632,3","1,0","6,3"
SUNFLOWER,"74,5","60,4","68,9","(7,5)","( 5,6)"
CASTOR BEAN,"128,2","87,5","87,4","(31,8)","( 40,8)"
TOTAL CORN,"15.178,1","15.686,2","15.817,4","4,2","639,3"
CORN 1ST CROP,"7.558,5","6.879,2","6.864,7","(9,2)","( 693,8)"
CORN 2ND CROP,"7.619,6","8.807,0","8.952,7","17,5","1.333,1"
SOYBEAN,"25.042,2","27.715,2","27.715,5","10,7","2.673,3"
SORGHUM,"786,9","836,4","836,4","6,3","49,5"
SUBTOTAL,"48.386,0","50.715,4","50.943,6","5,3","2.557,7"
OAT,"153,0","168,7","168,7","10,3","15,7"
CANOLA,"42,4","43,8","43,8","3,3","1,4"
RYE,"2,3","2,3","2,3",-,-
BARLEY,"88,4","102,8","102,8","16,3","14,4"
WHEAT,"2.166,2","1.895,4","1.895,4","(12,5)","( 270,8)"
TRITICALE,"46,9","48,0","48,0","2,3","1,1"
SUBTOTAL,"2.499,2","2.261,0","2.261,0","(9,5)","( 238,2)"
BRAZIL,"50.885,2","5 2.976,4","5 3.204,6","4,6","2.319,5"
"",HARVEST,VARIATION,,,
"","11/12(a)",12/13,Percentage,Absolute,
"","May/2013(b)","Jun/2013(c)",(c/a),(c-a),
COTTON,"1.393,4","886,7","894,9","(35,8)","( 498,5)"
TOTAL PEANUT,"93,9","100,6","100,2","6,7","6,3"
PEANUT 1ST CROP,"82,1","86,3","86,2","5,0","4,1"
PEANUT 2ND CROP,"11,8","14,3","14,0","18,6","2,2"
RICE,"2.426,7","2.389,7","2.396,0","(1,3)","( 30,7)"
TOTAL BEANS,"3.262,1","2.952,7","3.026,9","(7,2)","( 235,2)"
BEANS 1ST CROP,"1.241,4","1.122,6","1.122,9","(9,5)","( 118,5)"
BEANS 2ND CROP,"1.394,6","1.275,4","1.271,7","(8,8)","( 122,9)"
BEANS 3RD CROP,"626,1","554,7","632,3","1,0","6,3"
SUNFLOWER,"74,5","60,4","68,9","(7,5)","( 5,6)"
CASTOR BEAN,"128,2","87,5","87,4","(31,8)","( 40,8)"
TOTAL CORN,"15.178,1","15.686,2","15.817,4","4,2","639,3"
CORN 1ST CROP,"7.558,5","6.879,2","6.864,7","(9,2)","( 693,8)"
CORN 2ND CROP,"7.619,6","8.807,0","8.952,7","17,5","1.333,1"
SOYBEAN,"25.042,2","27.715,2","27.715,5","10,7","2.673,3"
SORGHUM,"786,9","836,4","836,4","6,3","49,5"
SUBTOTAL,"48.386,0","50.715,4","50.943,6","5,3","2.557,7"
OAT,"153,0","168,7","168,7","10,3","15,7"
CANOLA,"42,4","43,8","43,8","3,3","1,4"
RYE,"2,3","2,3","2,3",-,-
BARLEY,"88,4","102,8","102,8","16,3","14,4"
WHEAT,"2.166,2","1.895,4","1.895,4","(12,5)","( 270,8)"
TRITICALE,"46,9","48,0","48,0","2,3","1,1"
SUBTOTAL,"2.499,2","2.261,0","2.261,0","(9,5)","( 238,2)"
BRAZIL,"50.885,2","5 2.976,4","5 3.204,6","4,6","2.319,5"
Expand Down

0 comments on commit 0705bc6

Please sign in to comment.