2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.gpsbabel
;
19 import java
.util
.StringTokenizer
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.slc
.gis
.model
.FieldPosition
;
24 import org
.argeo
.slc
.jts
.PositionProvider
;
26 import com
.vividsolutions
.jts
.geom
.Coordinate
;
27 import com
.vividsolutions
.jts
.geom
.GeometryFactory
;
28 import com
.vividsolutions
.jts
.geom
.Point
;
30 public class GpsBabelPositionProvider
implements PositionProvider
{
31 private Log log
= LogFactory
.getLog(GpsBabelPositionProvider
.class);
33 private GpsBabelCall gpsBabelCall
;
35 private GeometryFactory geometryFactory
= new GeometryFactory();
37 private String inputFormat
= "garmin,get_posn";
38 private String inputFile
= "usb:";
40 private Boolean silentlyFailing
= false;
43 gpsBabelCall
= new GpsBabelCall(inputFormat
, inputFile
, "csv", "-");
46 public FieldPosition
currentPosition() {
48 if (gpsBabelCall
== null)
53 output
= gpsBabelCall
.function();
54 silentlyFailing
= false;
55 } catch (Exception e
) {
56 if (!silentlyFailing
) {
57 log
.warn(e
.getMessage()
59 + (e
.getCause() != null ? e
.getCause().getMessage()
61 if (log
.isTraceEnabled())
63 silentlyFailing
= true;
67 StringTokenizer st
= new StringTokenizer(output
, ",");
68 Double latitude
= Double
.parseDouble(st
.nextToken());
69 Double longitude
= Double
.parseDouble(st
.nextToken());
70 Point position
= geometryFactory
.createPoint(new Coordinate(longitude
,
72 return new FieldPosition(position
);
75 public void setInputFormat(String inputFormat
) {
76 this.inputFormat
= inputFormat
;
79 public void setInputFile(String inputFile
) {
80 this.inputFile
= inputFile
;