org.openstreetmap.atlas.checks.flag.FlaggedRelation Maven / Gradle / Ivy
package org.openstreetmap.atlas.checks.flag;
import java.util.Map;
import org.openstreetmap.atlas.geography.Location;
import org.openstreetmap.atlas.geography.Rectangle;
import org.openstreetmap.atlas.geography.atlas.items.AtlasObject;
import org.openstreetmap.atlas.geography.atlas.items.Relation;
import org.openstreetmap.atlas.geography.atlas.items.RelationMemberList;
import org.openstreetmap.atlas.geography.geojson.GeoJsonUtils;
import org.openstreetmap.atlas.tags.ISOCountryTag;
import org.openstreetmap.atlas.tags.RelationTypeTag;
import org.openstreetmap.atlas.tags.annotations.validation.Validators;
import com.google.gson.JsonObject;
/**
* A flag for a {@link Relation}
*
* @author sayas01
*/
public class FlaggedRelation extends FlaggedObject
{
private static final long serialVersionUID = 81887932468503688L;
private final Relation relation;
private final Map properties;
private final String country;
public FlaggedRelation(final Relation relation)
{
this.relation = relation;
this.properties = initProperties(relation);
this.country = initCountry(relation);
}
/**
* A GeoJSON representation of the flagged object.
*
* @param flagIdentifier
* We always will want to know the id of the flag associated with this flag object.
* @return GeoJSON representation of the flagged object.
*/
@Override
public JsonObject asGeoJsonFeature(final String flagIdentifier)
{
final JsonObject geoJsonGeometry = this.relation.asGeoJsonGeometry();
final JsonObject featureProperties = this.relation.getGeoJsonProperties();
featureProperties.addProperty("flag:id", flagIdentifier);
featureProperties.addProperty("flag:type", FlaggedRelation.class.getSimpleName());
return GeoJsonUtils.feature(geoJsonGeometry, featureProperties);
}
/**
* @return The bounds of the object.
*/
@Override
public Rectangle bounds()
{
return this.relation.bounds();
}
@Override
public boolean equals(final Object other)
{
return super.equals(other);
}
/**
* @return ISO country code of the country
*/
@Override
public String getCountry()
{
return this.country;
}
/**
* As relations itself do not have a geometry, the geometry of FlaggedRelation is set to null.
*
* @return flagged geometry
*/
@Override
public Iterable getGeometry()
{
return null;
}
/**
* @return flag key-value property map
*/
@Override
public Map getProperties()
{
return this.properties;
}
@Override
public int hashCode()
{
return super.hashCode();
}
/**
* Helper method to check FlaggedRelation is of multipolygon type
*
* @return true if the flagged relation is a multipolygon
*/
public boolean isMultipolygonRelation()
{
return Validators.isOfType(this.relation, RelationTypeTag.class,
RelationTypeTag.MULTIPOLYGON);
}
/**
* @return list of all members of the relation
*/
public RelationMemberList members()
{
return this.relation.members();
}
private String initCountry(final AtlasObject object)
{
final Map tags = object.getTags();
if (tags.containsKey(ISOCountryTag.KEY))
{
return tags.get(ISOCountryTag.KEY);
}
return ISOCountryTag.COUNTRY_MISSING;
}
/**
* Populate properties of relation
*
* @param relation
* @return map of relation tags with additional key-value properties
*/
private Map initProperties(final Relation relation)
{
final Map tags = relation.getTags();
tags.put(ITEM_IDENTIFIER_TAG, relation.getIdentifier() + "");
tags.put(OSM_IDENTIFIER_TAG, relation.getOsmIdentifier() + "");
tags.put(ITEM_TYPE_TAG, "Relation");
return tags;
}
}