Skip to content

Commit

Permalink
Fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kiselev-dv committed Sep 23, 2014
1 parent 81b841e commit 0f89b61
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
28 changes: 24 additions & 4 deletions Gazetteer/src/main/java/me/osm/gazetter/BoundariesFallbacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDateTime;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.externalsorting.ExternalSort;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.io.WKTReader;

public class BoundariesFallbacker {

private static final Logger log = LoggerFactory.getLogger(BoundariesFallbacker.class);

private String fallbackPath;
private HashSet<String> fallbackTypes;

Expand All @@ -35,7 +39,21 @@ public class BoundariesFallbacker {
private Map<String, String> cache = null;
private File file;

public BoundariesFallbacker(String fallbackPath, List<String> storeTypes) {
private volatile static BoundariesFallbacker instance = null;

public static BoundariesFallbacker getInstance(String fallbackPath, List<String> storeTypes) {
if(instance == null) {
synchronized (BoundariesFallbacker.class) {
if(instance == null) {
instance = new BoundariesFallbacker(fallbackPath, storeTypes);
}
}
}

return instance;
}

private BoundariesFallbacker(String fallbackPath, List<String> storeTypes) {

this.fallbackPath = StringUtils.stripToNull(fallbackPath);

Expand All @@ -55,6 +73,7 @@ public BoundariesFallbacker(String fallbackPath, List<String> storeTypes) {
try {
file = new File(this.fallbackPath);
if(file.exists()) {
log.info("Load boundaries from {}", fallbackPath);
buildCache();
}

Expand Down Expand Up @@ -100,10 +119,9 @@ public MultiPolygon getGeometry(String id) {
return (MultiPolygon)new WKTReader().read(wkt);
}
}


}
catch (Exception e) {
log.debug("Error duiring load geometry from fallback for {}. Error: {}", id, e.getMessage());
return null;
}

Expand All @@ -117,11 +135,13 @@ private void buildCache() throws IOException {
String[] split = StringUtils.split(s, '\t');
if(split != null && split.length == 3) {
String id = split[0];
String geom = split[1];
String geom = split[2];

cache.put(id, geom);
}
}

log.info("Loaded {} lines.", cache.size());
}
}

Expand Down
5 changes: 3 additions & 2 deletions Gazetteer/src/main/java/me/osm/gazetter/striper/Slicer.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ public void run(String poiCatalogPath, List<String> types, List<String> exclude,

if(typesSet.contains("all") || typesSet.contains("boundaries")) {
builders.add(new BoundariesBuilder(this,
new BoundariesFallbacker(boundariesFallbackIndex, boundariesFallbackTypes)));
BoundariesFallbacker.getInstance(boundariesFallbackIndex, boundariesFallbackTypes)));
}

if(typesSet.contains("all") || typesSet.contains("places")) {
builders.add(new PlaceBuilder(this, this));
builders.add(new PlaceBuilder(this, this,
BoundariesFallbacker.getInstance(boundariesFallbackIndex, boundariesFallbackTypes)));
}

if(typesSet.contains("all") || typesSet.contains("highways")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map.Entry;
import java.util.Set;

import me.osm.gazetter.BoundariesFallbacker;
import me.osm.gazetter.addresses.AddressesUtils;
import me.osm.gazetter.addresses.NamesMatcher;
import me.osm.gazetter.addresses.impl.NamesMatcherImpl;
Expand Down Expand Up @@ -89,8 +90,8 @@ public static final double moveBack(double x) {
return x - DEGREE_OFFSET;
}

public PlaceBuilder(PlacePointHandler slicer, BoundariesHandler handler) {
super(handler, null);
public PlaceBuilder(PlacePointHandler slicer, BoundariesHandler handler, BoundariesFallbacker fallback) {
super(handler, fallback);
this.handler = slicer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -282,5 +284,26 @@ public void islandWithinHoleWithDisjuncOuter() {
System.out.println("islandWithinHoleWithDisjuncOuter: " + mp.toString());
}

@Test
public void realWorldExample1() throws ParseException {

List<LineString> outers = new ArrayList<LineString>();
List<LineString> inners = new ArrayList<LineString>();

WKTReader reader = new WKTReader(f);
outers.add((LineString) reader.read("LINESTRING (42.8211884 45.3375219, 42.821406 45.3375338, "
+ " 42.821478 45.3375392, 42.8214348 45.3379158, 42.8214215 45.3379422)"));
//outers.add((LineString) reader.read("LINESTRING (42.821046 45.3378754, 42.8208881 45.3378474)"));
outers.add((LineString) reader.read("LINESTRING (42.8214215 45.3379422, 42.821046 45.3378754, "
+ "42.8208881 45.3378474, 42.8206616 45.3378149, 42.8205843 45.3378055, 42.8206528 45.3374546, "
+ "42.8208394 45.3374756, 42.8211884 45.3375219)"));

MultiPolygon mp = BuildUtils.buildMultyPolygon(new Relation(), outers, inners);

assertNotNull(mp);
System.out.println("realWorldExample1: " + mp.toString());
}



}

0 comments on commit 0f89b61

Please sign in to comment.