X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.gis%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fgpsbabel%2FGpsBabelPositionProvider.java;h=8d2ca18fddbb6dd5ef1c2f67809a4c93893c4598;hb=f0c7a3c02b2ab88af730c5abd735c8cda5276d03;hp=b77a71c5236c09a7356e982c526705f3e9ceea8f;hpb=d3a5063833293cf77e680aa856dc28533414f5c4;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/gpsbabel/GpsBabelPositionProvider.java b/runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/gpsbabel/GpsBabelPositionProvider.java index b77a71c52..8d2ca18fd 100644 --- a/runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/gpsbabel/GpsBabelPositionProvider.java +++ b/runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/gpsbabel/GpsBabelPositionProvider.java @@ -1,7 +1,26 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.argeo.slc.gpsbabel; import java.util.StringTokenizer; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.gis.model.FieldPosition; import org.argeo.slc.jts.PositionProvider; import com.vividsolutions.jts.geom.Coordinate; @@ -9,6 +28,8 @@ import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.Point; public class GpsBabelPositionProvider implements PositionProvider { + private Log log = LogFactory.getLog(GpsBabelPositionProvider.class); + private GpsBabelCall gpsBabelCall; private GeometryFactory geometryFactory = new GeometryFactory(); @@ -16,22 +37,39 @@ public class GpsBabelPositionProvider implements PositionProvider { private String inputFormat = "garmin,get_posn"; private String inputFile = "usb:"; + private Boolean silentlyFailing = false; + public void init() { gpsBabelCall = new GpsBabelCall(inputFormat, inputFile, "csv", "-"); } - public Point currentPosition() { + public FieldPosition currentPosition() { // lazy init if (gpsBabelCall == null) init(); - String output = gpsBabelCall.function(); + String output; + try { + output = gpsBabelCall.function(); + silentlyFailing = false; + } catch (Exception e) { + if (!silentlyFailing) { + log.warn(e.getMessage() + + ": " + + (e.getCause() != null ? e.getCause().getMessage() + : "")); + if (log.isTraceEnabled()) + e.printStackTrace(); + silentlyFailing = true; + } + return null; + } StringTokenizer st = new StringTokenizer(output, ","); Double latitude = Double.parseDouble(st.nextToken()); Double longitude = Double.parseDouble(st.nextToken()); Point position = geometryFactory.createPoint(new Coordinate(longitude, latitude)); - return position; + return new FieldPosition(position); } public void setInputFormat(String inputFormat) {