package btools.routingapp; import java.io.File; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import btools.router.RoutingEngine; import btools.router.OsmNodeNamed; import btools.router.OsmTrack; import btools.router.RoutingContext; public class BRouterWorker { public String segmentDir; public String profilePath; public String rawTrackPath; public List nogoList; public String getTrackFromParams(Bundle params) { String pathToFileResult = params.getString("pathToFileResult"); if (pathToFileResult != null) { File f = new File (pathToFileResult); File dir = f.getParentFile(); if (!dir.exists() || !dir.canWrite()){ return "file folder does not exists or can not be written!"; } } long maxRunningTime = 60000; String sMaxRunningTime = params.getString( "maxRunningTime" ); if ( sMaxRunningTime != null ) { maxRunningTime = Integer.parseInt( sMaxRunningTime ) * 1000; } RoutingContext rc = new RoutingContext(); rc.rawTrackPath = rawTrackPath; rc.localFunction = profilePath; String tiFormat = params.getString( "turnInstructionFormat" ); if ( tiFormat != null ) { if ( "osmand".equalsIgnoreCase( tiFormat ) ) { rc.turnInstructionMode = 3; } else if ( "locus".equalsIgnoreCase( tiFormat ) ) { rc.turnInstructionMode = 2; } } if ( nogoList != null ) { rc.prepareNogoPoints( nogoList ); rc.nogopoints = nogoList; } readNogos( params ); // add interface provides nogos RoutingEngine cr = new RoutingEngine( null, null, segmentDir, readPositions(params), rc ); cr.quite = true; cr.doRun( maxRunningTime ); // store new reference track if any // (can exist fot timeed-out search) if ( cr.getFoundRawTrack() != null ) { try { cr.getFoundRawTrack().writeBinary( rawTrackPath ); } catch( Exception e ) {} } if ( cr.getErrorMessage() != null ) { return cr.getErrorMessage(); } String format = params.getString("trackFormat"); boolean writeKml = format != null && "kml".equals( format ); OsmTrack track = cr.getFoundTrack(); if ( track != null ) { if ( pathToFileResult == null ) { if ( writeKml ) return track.formatAsKml(); return track.formatAsGpx(); } try { if ( writeKml ) track.writeKml(pathToFileResult); else track.writeGpx(pathToFileResult); } catch( Exception e ) { return "error writing file: " + e; } } return null; } private List readPositions( Bundle params ) { List wplist = new ArrayList(); double[] lats = params.getDoubleArray("lats"); double[] lons = params.getDoubleArray("lons"); if (lats == null || lats.length < 2 || lons == null || lons.length < 2) { throw new IllegalArgumentException( "we need two lat/lon points at least!" ); } for( int i=0; i