-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces JSI in favor of JTS Topology Suite (#226)
Closes #224
- Loading branch information
Showing
3 changed files
with
46 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 15 additions & 54 deletions
69
src/main/java/technology/tabula/RectangleSpatialIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
59 changes: 28 additions & 31 deletions
59
src/test/resources/technology/tabula/csv/spreadsheet_no_bounding_frame.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters