8000 Fix IndexedPointInAreaLocator and SortedPackedIntervalRTree to handle empty input by dr-jts · Pull Request #462 · locationtech/jts · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix IndexedPointInAreaLocator and SortedPackedIntervalRTree to handle empty input #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
package org.locationtech.jts.algorithm.locate;

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

Expand Down Expand Up @@ -106,11 +107,15 @@ public void visitItem(Object item)

private static class IntervalIndexedGeometry
{
private final SortedPackedIntervalRTree index= new SortedPackedIntervalRTree();
private boolean isEmpty = false;
private SortedPackedIntervalRTree index= new SortedPackedIntervalRTree();

public IntervalIndexedGeometry(Geometry geom)
{
init(geom);
if (geom.isEmpty())
isEmpty = true;
else
init(geom);
}

private void init(Geometry geom)
Expand All @@ -135,13 +140,18 @@ private void addLine(Coordinate[] pts)

public List query(double min, double max)
{
if (isEmpty)
return new ArrayList();

ArrayListVisitor visitor = new ArrayListVisitor();
index.query(min, max, visitor);
return visitor.getItems();
}

public void query(double min, double max, ItemVisitor visitor)
{
if (isEmpty)
return;
index.query(min, max, visitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
public class SortedPackedIntervalRTree
{
private List leaves = new ArrayList();

/**
* If root is null that indicates
* that the tree has not yet been built,
* OR nothing has been added to the tree.
* In both cases, the tree is still open for insertions.
*/
private IntervalRTreeNode root = null;

public SortedPackedIntervalRTree()
Expand All @@ -63,7 +70,15 @@ public void insert(double min, double max, Object item)

private void init()
{
// already built
if (root != null) return;

/**
* if leaves is empty then nothing has been inserted.
* In this case it is safe to leave the tree in an open state
*/
if (leaves.size() == 0) return;

buildRoot();
}

Expand Down Expand Up @@ -134,7 +149,11 @@ private void printNode(IntervalRTreeNode node)
public void query(double min, double max, ItemVisitor visitor)
{
init();


// if root is null tree must be empty
if (root == null)
return;

root.query(min, max, visitor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.locationtech.jts.algorithm.AbstractPointInRingTest;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Location;
import org.locationtech.jts.io.WKTReader;

import junit.textui.TestRunner;
Expand Down Expand Up @@ -43,4 +44,13 @@ protected void runPtInRing(int expectedLoc, Coordinate pt, String wkt)
assertEquals(expectedLoc, result);
}

/**
* See JTS GH Issue #19.
* Used to infinite-loop on empty geometries.
*
* @throws Exception
*/
public void testEmpty() throws Exception {
runPtInRing(Location.EXTERIOR, new Coordinate(0,0), "POLYGON EMPTY");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2016 Vivid Solutions.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
*
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.locationtech.jts.index.intervalrtree;

import org.locationtech.jts.index.ArrayListVisitor;

import junit.framework.TestCase;
import junit.textui.TestRunner;

public class SortedPackedIntervalRTreeTest extends TestCase {

public static void main(String args[]) {
TestRunner.run(SortedPackedIntervalRTreeTest.class);
}

public SortedPackedIntervalRTreeTest(String name) { super(name); }

/**
* See JTS GH Issue #19.
* Used to infinite-loop on empty geometries.
*
*/
public void testEmpty() {
SortedPackedIntervalRTree spitree = new SortedPackedIntervalRTree();
ArrayListVisitor visitor = new ArrayListVisitor();
spitree.query(0, 1, visitor);
}
}
0