Class FeatureCollectionTools
java.lang.Object
org.openjump.core.apitools.ToolToMakeYourLifeEasier
org.openjump.core.apitools.FeatureCollectionTools
Class to speed up handling of FeatureCollections (or lists of features) during progamming by implementing
a set of common tasks.
Most functions can be used in a static way, but on the other hand for each FeatureCollection a instance of this class can be invoked.
This might be more convenient, if there is more than one thing to do with the same feature collection.
- Version:
- $Rev$ [sstein] - 22.Feb.2009 - modified to work in OpenJUMP
- Author:
- Ole Rahn, Stefan Steiniger
FH Osnabrück - University of Applied Sciences Osnabrück,
Project: PIROL (2005),
Subproject: Daten- und Wissensmanagement
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescription -
Method Summary
Modifier and TypeMethodDescriptionstatic PirolFeatureCollectionaddAttributesToFeatureCollection(FeatureCollection fc, AttributeInfo[] attributeInfos) static PirolFeatureCollectionaddAttributesToFeatureCollection(FeatureCollection fc, AttributeInfo[] attributeInfos, boolean clearOriginalFeatureCollection) Adds multiple attributes to a FeatureCollectionstatic PirolFeatureCollectionaddAttributesToFeatureCollection(FeatureCollection fc, List<AttributeInfo> attributeInfos) Adds multiple attributes to a FeatureCollectionaddAttributesToFeatureCollection(List attributeInfos) Adds multiple attributes to the FeatureCollectionstatic PirolFeatureCollectionaddAttributeToFeatureCollection(FeatureCollection fc, AttributeInfo attrInfo) static PirolFeatureCollectionaddAttributeToFeatureCollection(FeatureCollection fc, AttributeInfo attrInfo, boolean clearOldFeatureCollection) static PirolFeatureCollectionaddAttributeToFeatureCollection(FeatureCollection fc, String attr, AttributeType at, Object defaultVal) Method to add a new attribute to an existing FeatureCollection.static PirolFeatureCollectionaddAttributeToFeatureCollection(FeatureCollection fc, String attr, AttributeType at, Object defaultVal, boolean clearOldFeatureCollection) Method to add a new attribute to an existing FeatureCollection.addAttributeToFeatureCollection(String attr, AttributeType at, Object defaultVal) static PirolFeatureCollectionapplyFormulaToFeatureCollection(FeatureCollection oldFc, AttributeInfo attrInfo, FormulaValue formula, boolean clearOldFeatureCollection) Method to apply a given formula to the given, new attribute of the given featureCollectionprotected static booleanattributeExistsInSchema(FeatureSchema fs, String attr) static PirolFeatureCollectiondeep copies a the FeatureSchema, the Features and their GeometriesconvertToPointFeature(Feature f, boolean accountForRings) Extracts all points from an input feature and returns them as list of point features.static FeaturecopyFeature(Feature feat) "deep copys" the given Featurestatic FeaturecopyFeatureAndSetFeatureSchema(Feature feat, FeatureSchema newFs) "deep copys" the given Feature and thereby sets the given feature schema.static voiddeleteFeatures(List<Feature> features, PlugInContext context) deletes the given features from the map.static Feature[]Converts a given FeatureCollection into an array of Feature, that can - by far - be faster iterated.static Feature[]FeatureCollection2FeatureArray(List<Feature> features) Converts a given list of features into an array of Feature, that can - by far - be faster iterated.static doublegetAritmeticMiddleForAttribute(Feature[] featArray, int attr) static doublegetAritmeticMiddleForAttribute(Feature[] features, String attr) static doublegetAritmeticMiddleForAttribute(List<Feature> features, int attr) static org.locationtech.jts.geom.GeometrygetCenterOfMass(Feature[] features) Calculates the center of mass for the gives features' geometries.static org.locationtech.jts.geom.EnvelopegetEnvelopeForFeatures(Feature[] features) Creates an envelope object for the features in the given array.getFeature(int fid) gets the Feature with the given FID.static FeaturegetFeatureFromCollection(Feature[] features, int fid) Get the feature with the specified ID from the arraystatic FeaturegetFeatureFromCollection(FeatureCollection features, int fid) Get the feature with the specified ID from the FeatureCollectionstatic FeaturegetFeatureFromCollection(List<Feature> features, int fid) Get the feature with the specified ID from the Liststatic Object[]getMeanOrModeForAttributes(Feature[] features, String[] attrs) Method to calculate means (or modes) for the attributes given.static double[]getMinMaxAttributeValue(Feature[] featArray, FeatureSchema fs, String attr) static double[]getMinMaxAttributeValue(FeatureCollection features, String attr) static ObjectgetModusForAttribute(Feature[] features, int attr) static ObjectgetModusForAttribute(Feature[] features, String attr) static doublegetNumericalAttributeValue(Feature f, String attributeName) gets the value of an attribute; it checks if the attribute is of double or int type, otherwise NaN is returned.static intgetNumOfDifferentAttributeValues(Feature[] features, int attr) static intgetNumOfDifferentAttributeValues(Feature[] features, String attr) getSetOfDifferentAttributeValues(Feature[] features, int attr) static SetgetSetOfDifferentAttributeValues(Feature[] features, String attr) static StatisticIndicesgetStatistics(Feature[] featArray, FeatureSchema fs, String attr) static doublegetSumAttributeValue(Feature[] featArray, FeatureSchema fs, String attr) static StringgetUniqueAttributeName(FeatureCollection fc, String attr) getUniqueAttributeName(String attr) getValueAppearancesCount(Feature[] features, int[] attrs) Counts the number of appearances of each value of the given attributes within the given features.static booleanstatic ObjectresizeArray(Object oldArray, int newSize) Reallocates an array with a new size, and copies the contents of the old array to the new array.sortFeatureListByAttributeBeginWithSmallest(List<Feature> features, String attributeNameForSorting) Sorts a list of features according to the values of a attribute.static Object[]sortFeaturesIntoListsByAttributeValue(FeatureCollection features, String idAttribute) Sorts features according to unique attribute values into different lists (on list for each unique attribute).
-
Field Details
-
fc
-
featureList
-
logger
-
-
Constructor Details
-
FeatureCollectionTools
-
FeatureCollectionTools
-
-
Method Details
-
getFeature
gets the Feature with the given FID.- Parameters:
fid- FID to look for- Returns:
- the feature
-
getFeatureFromCollection
-
getFeatureFromCollection
-
cloneFeatureCollection
deep copies a the FeatureSchema, the Features and their Geometries- Parameters:
fc- the FeatureCollection to copy- Returns:
- the new copied FeatureCollection
-
getFeatureFromCollection
Get the feature with the specified ID from the FeatureCollection- Parameters:
features- array with Feature objectsfid- the feature ID we are looking for- Returns:
- feature with specified FID if exists, else null
-
getAritmeticMiddleForAttribute
-
getMeanOrModeForAttributes
Method to calculate means (or modes) for the attributes given. If means or modes are calulated depends on the attribute type of each given attribute. This method is hopefully faster, than calculation each mean (mode) in an extra loop, if there are more means (modes) to calculate than one...- Parameters:
features- list of features to calculate mean/modes forattrs- array of attribute names to calculate mean/modes for- Returns:
- array of objects, representing means (or modes) --> e.g. Double, Integer or String objects.
-
getEnvelopeForFeatures
Creates an envelope object for the features in the given array. This is an easy way to get an envelope for a subset of a layer's features.- Parameters:
features- array of Feature object to create an envelope for- Returns:
- the envelope containing the given features
-
getAritmeticMiddleForAttribute
-
getAritmeticMiddleForAttribute
- Parameters:
featArray- array of features to calculate the mean forattr- index of attribute to calculate the mean for- Returns:
- the mean (double because a mean of integers will very likely be a double)
-
getCenterOfMass
Calculates the center of mass for the gives features' geometries. It's like getCentroid(), but puts out one center of mass for N features instead of N centers for N features.- Parameters:
features- the features- Returns:
- the point, representing the center of mass
-
getSetOfDifferentAttributeValues
-
getSetOfDifferentAttributeValues
-
getNumOfDifferentAttributeValues
-
getNumOfDifferentAttributeValues
- Parameters:
features- list of featuresattr- index of attribute- Returns:
- number of differnt values in the given features attributes or -1 if an error occurs
-
getModusForAttribute
-
getValueAppearancesCount
Counts the number of appearances of each value of the given attributes within the given features. (Somewhat like a histogram for each given attribute, but each histogram class is just a single value.)- Parameters:
features- array of Features to count value appearances forattrs- array of attribute indices of attributes to count value appearances for- Returns:
- Array of mappings of values to number of appearances
-
getModusForAttribute
-
deleteFeatures
deletes the given features from the map. It thereby creates an EditTransaction that enables the user to undo the deletion.- Parameters:
features- features to be deletedcontext- curr. PlugIn context
-
copyFeature
-
copyFeatureAndSetFeatureSchema
"deep copys" the given Feature and thereby sets the given feature schema. The new FeatureSchema should have the same attribute names for copying. The new FeatureSchmema can have less or more attributes.- Parameters:
feat- the feature to be copiednewFs- the new feature schema- Returns:
- copy of the feature
-
getUniqueAttributeName
-
getUniqueAttributeName
-
attributeExistsInSchema
-
addAttributeToFeatureCollection
public PirolFeatureCollection addAttributeToFeatureCollection(String attr, AttributeType at, Object defaultVal) -
isAttributeTypeNumeric
-
applyFormulaToFeatureCollection
public static PirolFeatureCollection applyFormulaToFeatureCollection(FeatureCollection oldFc, AttributeInfo attrInfo, FormulaValue formula, boolean clearOldFeatureCollection) Method to apply a given formula to the given, new attribute of the given featureCollection- Parameters:
oldFc- old FeatureCollection that will be replaced by the new oneattrInfo- information for the new attributeformula- the parsed formula- Returns:
- FeatureCollection containing the new attribute
-
addAttributeToFeatureCollection
public static PirolFeatureCollection addAttributeToFeatureCollection(FeatureCollection fc, AttributeInfo attrInfo) -
addAttributeToFeatureCollection
public static PirolFeatureCollection addAttributeToFeatureCollection(FeatureCollection fc, AttributeInfo attrInfo, boolean clearOldFeatureCollection) -
addAttributeToFeatureCollection
public static PirolFeatureCollection addAttributeToFeatureCollection(FeatureCollection fc, String attr, AttributeType at, Object defaultVal, boolean clearOldFeatureCollection) Method to add a new attribute to an existing FeatureCollection. Since there is no "legal" way to add an attribute to a FeatureCollection, this method creates (and returns) a new FeatureCollection with a new FeatureSchema to do this operation. If a layer is to be manipulated the new FeatureCollection has to be set as THE FeatureCollection for the layer...- Parameters:
fc- the old feature collectionattr- name of the new attributeat- type of the new attributedefaultVal- the initial value for the attribute, since we do not want NULL values in the attribute tableclearOldFeatureCollection- if true the old feature collection will be erased to save RAM- Returns:
- new FeatureCollection with a new FeatureSchema including the new attribute
-
addAttributeToFeatureCollection
public static PirolFeatureCollection addAttributeToFeatureCollection(FeatureCollection fc, String attr, AttributeType at, Object defaultVal) Method to add a new attribute to an existing FeatureCollection. Since there is no "legal" way to add an attribute to a FeatureCollection, this method creates (and returns) a new FeatureCollection with a new FeatureSchema to do this operation. If a layer is to be manipulated the new FeatureCollection has to be set as THE FeatureCollection for the layer...- Parameters:
fc- the old feature collectionattr- name of the new attributeat- type of the new attributedefaultVal- the initial value for the attribute, since we do not want NULL values in the attribute table- Returns:
- new FeatureCollection with a new FeatureSchema including the new attribute
-
addAttributesToFeatureCollection
Adds multiple attributes to the FeatureCollection- Parameters:
attributeInfos- list containing the attributes (as AttributeInfo objects) to be added- Returns:
- a new FeatureCollection containing the old and the new attributes
- See Also:
-
addAttributesToFeatureCollection
public static PirolFeatureCollection addAttributesToFeatureCollection(FeatureCollection fc, List<AttributeInfo> attributeInfos) Adds multiple attributes to a FeatureCollection- Parameters:
fc- the source feature collectionattributeInfos- list containing the attributes (as AttributeInfo objects) to be added- Returns:
- a new FeatureCollection containing the old and the new attributes
- See Also:
-
addAttributesToFeatureCollection
public static PirolFeatureCollection addAttributesToFeatureCollection(FeatureCollection fc, AttributeInfo[] attributeInfos) -
addAttributesToFeatureCollection
public static PirolFeatureCollection addAttributesToFeatureCollection(FeatureCollection fc, AttributeInfo[] attributeInfos, boolean clearOriginalFeatureCollection) Adds multiple attributes to a FeatureCollection- Parameters:
fc- the source feature collectionattributeInfos- array containing the attributes to be addedclearOriginalFeatureCollection- set true, if you want to save RAM by clearing the original FeatureCollection, false if you still want to use it.- Returns:
- a new FeatureCollection containing the old and the new attributes
-
resizeArray
-
getMinMaxAttributeValue
-
getSumAttributeValue
-
getMinMaxAttributeValue
-
FeatureCollection2FeatureArray
Converts a given FeatureCollection into an array of Feature, that can - by far - be faster iterated.- Parameters:
fc- the feature Collection- Returns:
- an array of the features of the feature collection
-
FeatureCollection2FeatureArray
-
convertToPointFeature
Extracts all points from an input feature and returns them as list of point features. Note: for closed Geometry objects the start and end point are extracted - i.e. the points may be overlap.- Parameters:
f- the Feature to convertaccountForRings- doesn't add a point for the last coordinate if the geometry is a ring (i.e. first point equals last point)- Returns:
- list of point features
-
sortFeaturesIntoListsByAttributeValue
public static Object[] sortFeaturesIntoListsByAttributeValue(FeatureCollection features, String idAttribute) Sorts features according to unique attribute values into different lists (on list for each unique attribute). TODO: enable sorting for String values- Parameters:
features- the Features to sortidAttribute- (must be Double or Integer)- Returns:
- Object[0]: an Array of ArrayLists containing features, Object[1]: an Array of int values containing the unique values
used for sorting. Can return null if wrong AttributeType. E.g. use
Object[] myReturns = FeatureCollectionTools.sortFeaturesIntoListsByAttributeValue(pointFeatures, idAttribute);
individualPts = (ArrayList[])myReturns[0];
int[] uniqueValues = (int[])myReturns[1];
Note that the order in which the lists are returned is not sorted!
-
getNumericalAttributeValue
gets the value of an attribute; it checks if the attribute is of double or int type, otherwise NaN is returned.- Parameters:
f- the Feature from which to extract the numeric attributeattributeName- name of the attribute to extract- Returns:
- value as double
-
sortFeatureListByAttributeBeginWithSmallest
public static ArrayList<Feature> sortFeatureListByAttributeBeginWithSmallest(List<Feature> features, String attributeNameForSorting) Sorts a list of features according to the values of a attribute. If values are similar the feature are ordered the same way as in the input list. TODO: this method has been tested only briefly (not exhaustively)- Parameters:
features- the features to sortattributeNameForSorting- attribute needs to be either Integer or Double- Returns:
- list of sorted features; the smallest value will be first in the list.
-
getStatistics
-