Skip to content

Commit

Permalink
restructuring of code
Browse files Browse the repository at this point in the history
moving ElementsResult filling into ExecutionUtils to decrease code
  • Loading branch information
kowatsch committed May 24, 2018
1 parent e36cbd1 commit 87a84d4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,7 @@ public static DefaultAggregationResponse executeCountLengthPerimeterArea(
}
GeometryBuilder geomBuilder = iP.getGeomBuilder();
Geometry geom = exeUtils.getGeometry(iP.getBoundaryType(), geomBuilder);
int count = 0;
ElementsResult[] resultSet = new ElementsResult[result.size()];
for (Entry<OSHDBTimestamp, ? extends Number> entry : result.entrySet()) {
if (rPs.isDensity()) {
resultSet[count] = new ElementsResult(
TimestampFormatter.getInstance().isoDateTime(entry.getKey()), Double.parseDouble(
df.format((entry.getValue().doubleValue() / (Geo.areaOf(geom) * 0.000001)))));
} else {
resultSet[count] =
new ElementsResult(TimestampFormatter.getInstance().isoDateTime(entry.getKey()),
Double.parseDouble(df.format((entry.getValue().doubleValue()))));
}
count++;
}
ElementsResult[] resultSet = exeUtils.fillElementsResult(result, rPs.isDensity(), df, geom);
Metadata metadata = null;
if (iP.getShowMetadata()) {
long duration = System.currentTimeMillis() - startTime;
Expand Down Expand Up @@ -192,18 +179,11 @@ public static GroupByResponse executeCountLengthPerimeterAreaGroupByBoundary(
Utils utils = iP.getUtils();
String[] boundaryIds = utils.getBoundaryIds();
int count = 0;
int innerCount = 0;
for (Entry<Integer, ? extends SortedMap<OSHDBTimestamp, ? extends Number>> entry : groupByResult
.entrySet()) {
ElementsResult[] results = new ElementsResult[entry.getValue().entrySet().size()];
innerCount = 0;
ElementsResult[] results =
exeUtils.fillElementsResult(entry.getValue(), rPs.isDensity(), df, null);
groupByName = boundaryIds[count];
for (Entry<OSHDBTimestamp, ? extends Number> innerEntry : entry.getValue().entrySet()) {
results[innerCount] =
new ElementsResult(TimestampFormatter.getInstance().isoDateTime(innerEntry.getKey()),
Double.parseDouble(df.format(innerEntry.getValue().doubleValue())));
innerCount++;
}
resultSet[count] = new GroupByResult(groupByName, results);
count++;
}
Expand Down Expand Up @@ -281,21 +261,17 @@ public static GroupByResponse executeCountLengthPerimeterAreaGroupByUser(
}
groupByResult = MapAggregatorByTimestampAndIndex.nest_IndexThenTime(result);
GroupByResult[] resultSet = new GroupByResult[groupByResult.size()];

int count = 0;
int innerCount = 0;
for (Entry<Integer, ? extends SortedMap<OSHDBTimestamp, ? extends Number>> entry : groupByResult
.entrySet()) {
ElementsResult[] results = new ElementsResult[entry.getValue().entrySet().size()];
innerCount = 0;
for (Entry<OSHDBTimestamp, ? extends Number> innerEntry : entry.getValue().entrySet()) {
results[innerCount] =
new ElementsResult(TimestampFormatter.getInstance().isoDateTime(innerEntry.getKey()),
Double.parseDouble(df.format(innerEntry.getValue().doubleValue())));
innerCount++;
}

ElementsResult[] results =
exeUtils.fillElementsResult(entry.getValue(), rPs.isDensity(), df, null);
resultSet[count] = new GroupByResult(entry.getKey().toString(), results);
count++;
}

Metadata metadata = null;
if (iP.getShowMetadata()) {
long duration = System.currentTimeMillis() - startTime;
Expand Down Expand Up @@ -403,25 +379,14 @@ public static GroupByResponse executeCountLengthPerimeterAreaGroupByTag(
int count = 0;
for (Entry<Pair<Integer, Integer>, ? extends SortedMap<OSHDBTimestamp, ? extends Number>> entry : groupByResult
.entrySet()) {
ElementsResult[] results = new ElementsResult[entry.getValue().entrySet().size()];
int innerCount = 0;
ElementsResult[] results =
exeUtils.fillElementsResult(entry.getValue(), rPs.isDensity(), df, geom);
// check for non-remainder objects (which do have the defined key and value)
if (entry.getKey().getKey() != -1 && entry.getKey().getValue() != -1) {
groupByName = tt.getOSMTagOf(keysInt, entry.getKey().getValue()).toString();
} else {
groupByName = "remainder";
}
for (Entry<OSHDBTimestamp, ? extends Number> innerEntry : entry.getValue().entrySet()) {
if (rPs.isDensity())
results[innerCount] = new ElementsResult(
TimestampFormatter.getInstance().isoDateTime(innerEntry.getKey()), Double.parseDouble(
df.format((innerEntry.getValue().doubleValue() / (Geo.areaOf(geom) / 1000000)))));
else
results[innerCount] =
new ElementsResult(TimestampFormatter.getInstance().isoDateTime(innerEntry.getKey()),
Double.parseDouble(df.format(innerEntry.getValue().doubleValue())));
innerCount++;
}
resultSet[count] = new GroupByResult(groupByName, results);
count++;
}
Expand Down Expand Up @@ -505,22 +470,10 @@ public static GroupByResponse executeCountPerimeterAreaGroupByType(
GeometryBuilder geomBuilder = iP.getGeomBuilder();
Geometry geom = exeUtils.getGeometry(iP.getBoundaryType(), geomBuilder);
int count = 0;
int innerCount = 0;
for (Entry<OSMType, ? extends SortedMap<OSHDBTimestamp, ? extends Number>> entry : groupByResult
.entrySet()) {
ElementsResult[] results = new ElementsResult[entry.getValue().entrySet().size()];
innerCount = 0;
for (Entry<OSHDBTimestamp, ? extends Number> innerEntry : entry.getValue().entrySet()) {
if (rPs.isDensity())
results[innerCount] = new ElementsResult(
TimestampFormatter.getInstance().isoDateTime(innerEntry.getKey()), Double.parseDouble(
df.format((innerEntry.getValue().doubleValue() / (Geo.areaOf(geom) / 1000000)))));
else
results[innerCount] =
new ElementsResult(TimestampFormatter.getInstance().isoDateTime(innerEntry.getKey()),
Double.parseDouble(df.format(innerEntry.getValue().doubleValue())));
innerCount++;
}
ElementsResult[] results =
exeUtils.fillElementsResult(entry.getValue(), rPs.isDensity(), df, geom);
resultSet[count] = new GroupByResult(entry.getKey().toString(), results);
count++;
}
Expand Down Expand Up @@ -617,23 +570,16 @@ public static GroupByResponse executeCountLengthPerimeterAreaGroupByKey(
GroupByResult[] resultSet = new GroupByResult[groupByResult.size()];
String groupByName = "";
int count = 0;
int innerCount = 0;
for (Entry<Integer, ? extends SortedMap<OSHDBTimestamp, ? extends Number>> entry : groupByResult
.entrySet()) {
ElementsResult[] results = new ElementsResult[entry.getValue().entrySet().size()];
innerCount = 0;
ElementsResult[] results =
exeUtils.fillElementsResult(entry.getValue(), rPs.isDensity(), df, null);
// check for non-remainder objects (which do have the defined key)
if (entry.getKey() != -1) {
groupByName = tt.getOSMTagKeyOf(entry.getKey().intValue()).toString();
} else {
groupByName = "remainder";
}
for (Entry<OSHDBTimestamp, ? extends Number> innerEntry : entry.getValue().entrySet()) {
results[innerCount] =
new ElementsResult(TimestampFormatter.getInstance().isoDateTime(innerEntry.getKey()),
Double.parseDouble(df.format(innerEntry.getValue().doubleValue())));
innerCount++;
}
resultSet[count] = new GroupByResult(groupByName, results);
count++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.SortedMap;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.heigit.bigspatialdata.ohsome.ohsomeApi.exception.BadRequestException;
import org.heigit.bigspatialdata.ohsome.ohsomeApi.inputProcessing.BoundaryType;
import org.heigit.bigspatialdata.ohsome.ohsomeApi.inputProcessing.GeometryBuilder;
import org.heigit.bigspatialdata.ohsome.ohsomeApi.output.dataAggregationResponse.elements.ElementsResult;
import org.heigit.bigspatialdata.oshdb.api.generic.OSHDBTimestampAndIndex;
import org.heigit.bigspatialdata.oshdb.api.mapreducer.MapAggregator;
import org.heigit.bigspatialdata.oshdb.api.mapreducer.MapReducer;
Expand All @@ -21,8 +23,10 @@
import org.heigit.bigspatialdata.oshdb.api.object.OSMEntitySnapshot;
import org.heigit.bigspatialdata.oshdb.osm.OSMEntity;
import org.heigit.bigspatialdata.oshdb.osm.OSMType;
import org.heigit.bigspatialdata.oshdb.util.OSHDBTimestamp;
import org.heigit.bigspatialdata.oshdb.util.geometry.Geo;
import org.heigit.bigspatialdata.oshdb.util.geometry.OSHDBGeometryBuilder;
import org.heigit.bigspatialdata.oshdb.util.time.TimestampFormatter;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Polygonal;

Expand Down Expand Up @@ -284,6 +288,27 @@ public boolean entityMatches(OSMEntity entity, EnumSet<OSMType> osmTypes, Intege
return matches;
}

public ElementsResult[] fillElementsResult(SortedMap<OSHDBTimestamp, ? extends Number> entryVal,
boolean isDensity, DecimalFormat df, Geometry geom) {

ElementsResult[] results = new ElementsResult[entryVal.entrySet().size()];
int count = 0;
for (Entry<OSHDBTimestamp, ? extends Number> entry : entryVal.entrySet()) {
if (isDensity) {
results[count] = new ElementsResult(
TimestampFormatter.getInstance().isoDateTime(entry.getKey()), Double.parseDouble(
df.format((entry.getValue().doubleValue() / (Geo.areaOf(geom) * 0.000001)))));
} else {
results[count] =
new ElementsResult(TimestampFormatter.getInstance().isoDateTime(entry.getKey()),
Double.parseDouble(df.format((entry.getValue().doubleValue()))));
}
count++;
}

return results;
}

/** Enum type used in /ratio computation. */
public enum MatchType {
MATCHES1, MATCHES2, MATCHESBOTH, MATCHESNONE
Expand Down

0 comments on commit 87a84d4

Please sign in to comment.