]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/gpsbabel/GpsBabelPositionProvider.java
Improve GPX importer
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.gis / src / main / java / org / argeo / slc / gpsbabel / GpsBabelPositionProvider.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.gpsbabel;
18
19 import java.util.StringTokenizer;
20
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;
25
26 import com.vividsolutions.jts.geom.Coordinate;
27 import com.vividsolutions.jts.geom.GeometryFactory;
28 import com.vividsolutions.jts.geom.Point;
29
30 public class GpsBabelPositionProvider implements PositionProvider {
31 private Log log = LogFactory.getLog(GpsBabelPositionProvider.class);
32
33 private GpsBabelCall gpsBabelCall;
34
35 private GeometryFactory geometryFactory = new GeometryFactory();
36
37 private String inputFormat = "garmin,get_posn";
38 private String inputFile = "usb:";
39
40 private Boolean silentlyFailing = false;
41
42 public void init() {
43 gpsBabelCall = new GpsBabelCall(inputFormat, inputFile, "csv", "-");
44 }
45
46 public FieldPosition currentPosition() {
47 // lazy init
48 if (gpsBabelCall == null)
49 init();
50
51 String output;
52 try {
53 output = gpsBabelCall.function();
54 silentlyFailing = false;
55 } catch (Exception e) {
56 if (!silentlyFailing) {
57 log.warn(e.getMessage()
58 + ": "
59 + (e.getCause() != null ? e.getCause().getMessage()
60 : ""));
61 if (log.isTraceEnabled())
62 e.printStackTrace();
63 silentlyFailing = true;
64 }
65 return null;
66 }
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,
71 latitude));
72 return new FieldPosition(position);
73 }
74
75 public void setInputFormat(String inputFormat) {
76 this.inputFormat = inputFormat;
77 }
78
79 public void setInputFile(String inputFile) {
80 this.inputFile = inputFile;
81 }
82
83 }