MATSIM
CoordConverter.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * CoordConverter.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2019 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 
22  package org.matsim.utils.objectattributes.attributeconverters;
23 
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.matsim.api.core.v01.Coord;
28 
29 public class CoordConverter implements AttributeConverter<Coord> {
30  private final Logger log = LogManager.getLogger(CoordConverter.class);
31 
32  @Override
33  public Coord convert(String value) {
34  String s = value.replace("(", "");
35  s = s.replace(")", "");
36  String[] sa = s.split(";");
37  return new Coord(Double.parseDouble(sa[0]), Double.parseDouble(sa[1]));
38  }
39 
40  @Override
41  public String convertToString(Object o) {
42  if(!(o instanceof Coord)){
43  log.error("Object is not of type Coord: " + o.getClass().toString());
44  return null;
45  }
46  Coord c = (Coord)o;
47 
48  return String.format("(%s;%s)", Double.toString(c.getX()), Double.toString(c.getY()));
49  }
50 
51 }