diff --git a/brouter-core/src/main/java/btools/router/OsmNogoPolygon.java b/brouter-core/src/main/java/btools/router/OsmNogoPolygon.java index 3014cd8..bf6fed6 100644 --- a/brouter-core/src/main/java/btools/router/OsmNogoPolygon.java +++ b/brouter-core/src/main/java/btools/router/OsmNogoPolygon.java @@ -29,8 +29,8 @@ public class OsmNogoPolygon extends OsmNodeNamed { public final static class Point { - final int y; - final int x; + public final int y; + public final int x; Point(final int lon, final int lat) { diff --git a/brouter-core/src/main/java/btools/router/RoutingContext.java b/brouter-core/src/main/java/btools/router/RoutingContext.java index 2a66f0b..5c28da5 100644 --- a/brouter-core/src/main/java/btools/router/RoutingContext.java +++ b/brouter-core/src/main/java/btools/router/RoutingContext.java @@ -192,16 +192,20 @@ public final class RoutingContext { for( OsmNodeNamed nogo : nogos ) { - String s = nogo.name; - int idx = s.indexOf( ' ' ); - if ( idx > 0 ) s = s.substring( 0 , idx ); - int ir = 20; // default radius - if ( s.length() > 4 ) - { - try { ir = Integer.parseInt( s.substring( 4 ) ); } - catch( Exception e ) { /* ignore */ } - } - nogo.radius = ir / 110984.; // 6378000. / 57.3; + if (nogo instanceof OsmNogoPolygon) + { + continue; + } + String s = nogo.name; + int idx = s.indexOf( ' ' ); + if ( idx > 0 ) s = s.substring( 0 , idx ); + int ir = 20; // default radius + if ( s.length() > 4 ) + { + try { ir = Integer.parseInt( s.substring( 4 ) ); } + catch( Exception e ) { /* ignore */ } + } + nogo.radius = ir / 110984.; // 6378000. / 57.3; } } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index d611000..d532fe0 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -32,6 +32,8 @@ import btools.expressions.BExpressionContextWay; import btools.expressions.BExpressionMetaData; import btools.mapaccess.OsmNode; import btools.router.OsmNodeNamed; +import btools.router.OsmNogoPolygon; +import btools.router.OsmNogoPolygon.Point; import btools.router.OsmTrack; import btools.router.RoutingContext; import btools.router.RoutingEngine; @@ -518,7 +520,7 @@ public class BRouterView extends View scaleLon = scaleLat * coslat; startTime = System.currentTimeMillis(); - rc.prepareNogoPoints( nogoList ); + RoutingContext.prepareNogoPoints( nogoList ); rc.nogopoints = nogoList; rc.memoryclass = memoryClass; @@ -640,6 +642,41 @@ public class BRouterView extends View canvas.drawCircle( (float) x, (float) y, (float) ir, paint ); } } + + private void paintLine( Canvas canvas, final int ilon0, final int ilat0, final int ilon1, final int ilat1, final Paint paint ) + { + final int lon0 = ilon0 - centerLon; + final int lat0 = ilat0 - centerLat; + final int lon1 = ilon1 - centerLon; + final int lat1 = ilat1 - centerLat; + final int x0 = imgw / 2 + (int) ( scaleLon * lon0 ); + final int y0 = imgh / 2 - (int) ( scaleLat * lat0 ); + final int x1 = imgw / 2 + (int) ( scaleLon * lon1 ); + final int y1 = imgh / 2 - (int) ( scaleLat * lat1 ); + canvas.drawLine( (float) x0, (float) y0, (float) x1, (float) y1, paint ); + } + + private void paintPolygon( Canvas canvas, OsmNogoPolygon p, int minradius ) + { + final int ir = (int) ( p.radius * 1000000. * scaleLat ); + if ( ir > minradius ) + { + Paint paint = new Paint(); + paint.setColor( Color.RED ); + paint.setStyle( Paint.Style.STROKE ); + + Point p0 = p.isClosed ? p.points.get(p.points.size()-1) : null; + + for ( final Point p1 : p.points ) + { + if (p0 != null) + { + paintLine( canvas, p0.x, p0.y, p1.x, p1.y, paint ); + } + p0 = p1; + } + } + } @Override protected void onSizeChanged( int w, int h, int oldw, int oldh ) @@ -824,8 +861,15 @@ public class BRouterView extends View for ( int ngi = 0; ngi < nogoList.size(); ngi++ ) { OsmNodeNamed n = nogoList.get( ngi ); - int color = 0xff0000; - paintCircle( canvas, n, color, 4 ); + if (n instanceof OsmNogoPolygon) + { + paintPolygon( canvas, (OsmNogoPolygon)n, 4 ); + } + else + { + int color = 0xff0000; + paintCircle( canvas, n, color, 4 ); + } } Paint paint = new Paint(); diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java index 917c45a..7824d48 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java @@ -3,9 +3,15 @@ package btools.routingapp; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; import java.io.InputStreamReader; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserFactory; + import btools.router.OsmNodeNamed; +import btools.router.OsmNogoPolygon; /** * Read coordinates from a gpx-file @@ -65,6 +71,13 @@ public class CoordinateReaderOsmAnd extends CoordinateReader { _readPointmap( osmandDir + "/favourites.gpx" ); } + try + { + _readNogoLines( basedir+tracksdir ); + } + catch( IOException ioe ) + { + } } private void _readPointmap( String filename ) throws Exception @@ -107,4 +120,71 @@ public class CoordinateReaderOsmAnd extends CoordinateReader } br.close(); } + + private void _readNogoLines( String dirname ) throws IOException + { + + File dir = new File( dirname ); + + if (dir.exists() && dir.isDirectory()) + { + for (final File file : dir.listFiles()) + { + final String name = file.getName(); + if (name.startsWith("nogo") && name.endsWith(".gpx")) + { + try + { + _readNogoLine(file); + } + catch (Exception e) + { + } + } + } + } + } + + private void _readNogoLine( File file ) throws Exception + { + XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); + factory.setNamespaceAware(false); + XmlPullParser xpp = factory.newPullParser(); + + xpp.setInput(new FileReader(file)); + OsmNogoPolygon nogo = new OsmNogoPolygon(true); + int eventType = xpp.getEventType(); + int numSeg = 0; + while (eventType != XmlPullParser.END_DOCUMENT) { + switch(eventType) { + case XmlPullParser.START_TAG: { + if (xpp.getName().equals("trkpt")) { + final String lon = xpp.getAttributeValue(null,"lon"); + final String lat = xpp.getAttributeValue(null,"lat"); + if (lon != null && lat != null) { + nogo.addVertex( + (int)( ( Double.parseDouble(lon) + 180. ) *1000000. + 0.5), + (int)( ( Double.parseDouble(lat) + 90. ) *1000000. + 0.5)); + } + } + break; + } + case XmlPullParser.END_TAG: { + if (xpp.getName().equals("trkseg")) { + nogo.calcBoundingCircle(); + final String name = file.getName(); + nogo.name = name.substring(0, name.length()-4); + if (numSeg > 0) + { + nogo.name += Integer.toString(numSeg+1); + } + numSeg++; + checkAddPoint( "(one-for-all)", nogo ); + } + break; + } + } + eventType = xpp.next(); + } + } }