Skip to content
Open
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 @@ -12,6 +12,7 @@
package org.locationtech.jts.geom.util;

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

import org.locationtech.jts.geom.Geometry;
Expand All @@ -33,14 +34,55 @@
public class GeometryExtracter
implements GeometryFilter
{
/**
* Extracts the components of type <tt>clz</tt> from a {@link Geometry}
* and adds them to the provided {@link List}.
*
* @param geom the geometry from which to extract
* @param list the list to add the extracted elements to
* @deprecated Use {@link GeometryExtracter#extract(Geometry, String, List)}
*/

/**
* Extracts the components of type {@code clz} from a {@link Geometry}, returns
* them in a new {@link List}, and optionally also adds them to {@code out}.
* <p>
* This method is intentionally NOT named {@code extract(...)} to avoid overload
* ambiguity with the legacy JTS {@code extract(Geometry, String, List)} /
* {@code extract(Geometry, Class, List)} methods.
*
* @param geom the geometry from which to extract (may be {@code null})
* @param clz the class of the components to extract (must not be {@code null})
* @param out optional collection to also write extracted components into (may
* be {@code null})
* @param <T> the component type
* @return a new modifiable list of extracted components
*/
public static <T extends Geometry> List<T> extractByClass(Geometry geom, Class<T> clz, Collection<? super T> out) {
List<T> result = new ArrayList<T>();
if (geom == null)
return result;

geom.apply(new GeometryExtracter(clz, result));
if (out != null)
out.addAll(result);

return result;
}

/**
* Extracts the components of type {@code clz} from a {@link Geometry} and
* returns them in a new {@link List}.
*
* @param geom the geometry from which to extract (may be {@code null})
* @param clz the class of the components to extract (must not be {@code null})
* @param <T> the component type
* @return a new modifiable list of extracted components
*/
public static <T extends Geometry> List<T> extractByClass(Geometry geom, Class<T> clz) {
return extractByClass(geom, clz, null);
}

/**
* Extracts the components of type <tt>clz</tt> from a {@link Geometry} and adds
* them to the provided {@link List}.
*
* @param geom the geometry from which to extract
* @param list the list to add the extracted elements to
* @deprecated Use {@link GeometryExtracter#extract(Geometry, String, List)}
*/
public static List extract(Geometry geom, Class clz, List list)
{
return extract(geom, toGeometryType(clz), list);
Expand Down Expand Up @@ -109,44 +151,50 @@ public static List extract(Geometry geom, String geometryType)
return extract(geom, geometryType, new ArrayList());
}

private String geometryType;
private List comps;

/**
* Constructs a filter with a list in which to store the elements found.
*
* @param clz the class of the components to extract (null means all types)
* @param comps the list to extract into
* @deprecated
*/
public GeometryExtracter(Class clz, List comps)
private final String geometryType; // legacy string-based matching mode
private final Class<?> componentClass; // class-based matching mode
private final Collection comps;

public GeometryExtracter(String geometryType, List comps)
{
this.geometryType = toGeometryType(clz);
this.geometryType = geometryType;
this.componentClass = null;
this.comps = comps;
}

/**
* Constructs a filter with a list in which to store the elements found.
*
* @param geometryType Geometry type to extract (null means all types)
* Constructs a filter matching by component class.
*
* @param componentClass the class of the components to extract (null means all types)
* @param comps the list to extract into
*/
public GeometryExtracter(String geometryType, List comps)
public GeometryExtracter(Class componentClass, List comps)
{
this.geometryType = geometryType;
this.geometryType = null;
this.componentClass = componentClass;
this.comps = comps;
}


@Override
public void filter(Geometry geom)
{
if (geom == null) return;

if (componentClass != null) {
if (componentClass.isInstance(geom)) comps.add(geom);
return;
}

// legacy string-based behaviour
if (geometryType == null || isOfType(geom, geometryType)) {
comps.add(geom);
}
}

protected static boolean isOfType(Geometry geom, String geometryType) {
if (geom.getGeometryType() == geometryType) return true;
if (geometryType == Geometry.TYPENAME_LINESTRING
&& geom.getGeometryType() == Geometry.TYPENAME_LINEARRING) return true;
&& geom.getGeometryType() == Geometry.TYPENAME_LINEARRING) return true;
return false;
}

public void filter(Geometry geom) {
if (geometryType == null || isOfType(geom, geometryType))
comps.add(geom);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,75 @@
*/
package org.locationtech.jts.geom.util;

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

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFilter;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.MultiLineString;

/**
* Extracts all the {@link LineString} elements from a {@link Geometry}.
*
* Extracts {@link LineString} components from a {@link Geometry}.
* <p>
* This class implements {@link GeometryFilter} so it can be passed to
* {@link Geometry#apply(GeometryFilter)}.
*
* @version 1.7
* @see GeometryExtracter
* @see org.locationtech.jts.geom.util.GeometryExtracter GeometryExtracter
*/
public class LineStringExtracter
implements GeometryFilter
{
/**
* Extracts the {@link LineString} elements from a single {@link Geometry}
* and adds them to the provided {@link List}.
*
* @param geom the geometry from which to extract
* @param lines the list to add the extracted LineStrings to
* @return the list argument
*/
public static List getLines(Geometry geom, List lines)
{
if (geom instanceof LineString) {
lines.add(geom);
}
else if (geom instanceof GeometryCollection) {
geom.apply(new LineStringExtracter(lines));
}
// skip non-LineString elemental geometries

return lines;
}
public final class LineStringExtracter implements GeometryFilter {
/**
* Extracts the {@link LineString} elements from a single {@link Geometry} and
* adds them to the provided {@link Collection}.
*
* @param geom the geometry from which to extract (may be {@code null})
* @param out an optional collection to add the extracted LineStrings to (may
* be {@code null})
* @return a new modifiable {@link List} containing the extracted LineStrings
*/
public static List<LineString> getLines(Geometry geom, Collection<? super LineString> out) {
return GeometryExtracter.extractByClass(geom, LineString.class, out);
}

/**
* Extracts the {@link LineString} elements from a single {@link Geometry}
* and returns them in a {@link List}.
*
* @param geom the geometry from which to extract
* @return a list containing the linear elements
*/
public static List getLines(Geometry geom)
{
return getLines(geom, new ArrayList());
}
/**
* Extracts the {@link LineString} elements from a single {@link Geometry} and
* returns them in a {@link List}.
*
* @param geom the geometry from which to extract (may be {@code null})
* @return a new modifiable {@link List} containing the linear elements
*/
public static List<LineString> getLines(Geometry geom) {
return GeometryExtracter.extractByClass(geom, LineString.class);
}

/**
* Extracts the {@link LineString} elements from a single {@link Geometry}
* and returns them as either a {@link LineString} or {@link MultiLineString}.
*
* @param geom the geometry from which to extract
* @return a linear geometry
*/
public static Geometry getGeometry(Geometry geom)
{
return geom.getFactory().buildGeometry(getLines(geom));
}
/**
* Extracts the {@link LineString} elements from a single {@link Geometry} and
* returns them as either a {@link LineString} or {@link MultiLineString} (or an
* empty geometry if none are present).
*
* @param geom the geometry from which to extract
* @return a linear geometry
*/
public static Geometry getGeometry(Geometry geom) {
return geom.getFactory().buildGeometry(getLines(geom));
}

private List comps;

/**
* Constructs a filter with a list in which to store the elements found.
*/
public LineStringExtracter(List comps)
{
this.comps = comps;
}
private final Collection<? super LineString> comps;

public void filter(Geometry geom)
{
if (geom instanceof LineString) comps.add(geom);
}
/**
* Constructs a filter with a collection in which to store {@link LineString}s
* found.
*
* @param comps the collection in which to store LineStrings found
*/
public LineStringExtracter(Collection<? super LineString> comps) {
this.comps = comps;
}

}
@Override
public void filter(Geometry geom) {
if (geom instanceof LineString)
comps.add((LineString) geom);
}
}
Loading