MATSIM
HullConverter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2013 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.core.network.algorithms.intersectionSimplifier;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.locationtech.jts.geom.Coordinate;
28 import org.locationtech.jts.geom.Geometry;
29 import org.locationtech.jts.geom.GeometryFactory;
30 import org.locationtech.jts.geom.Polygon;
35 
44 public class HullConverter implements AttributeConverter<Geometry> {
45  private final Logger log = LogManager.getLogger(HullConverter.class);
46 
47  @Override
48  public Geometry convert(String value) {
49  GeometryFactory gf = new GeometryFactory();
50  Geometry g;
51 
52  List<Coordinate> list = new ArrayList<Coordinate>();
53  String[] sa = value.split(",");
54  for(String s : sa){
55  String[] sa2 = s.substring(1, s.length()-1).split(";");
56  double x = Double.parseDouble(sa2[0]);
57  double y = Double.parseDouble(sa2[1]);
58  list.add(new Coordinate(x, y));
59  }
60 
61  Coordinate[] ca = new Coordinate[list.size()];
62 
63  /* Distinguish between points, lines and polygons. */
64  if(ca.length == 1){
65  ca[0] = list.getFirst();
66  g = gf.createPoint(ca[0]);
67  } else if(ca.length == 2){
68  ca[0] = list.get(0);
69  ca[1] = list.get(1);
70  g = gf.createLineString(ca);
71  } else{
72  for(int i = 0; i < list.size(); i++){
73  ca[i] = list.get(i);
74  }
75  g = gf.createPolygon(gf.createLinearRing(ca), null);
76  }
77 
78  return g;
79  }
80 
81  @Override
82  public String convertToString(Object o) {
83  if(!(o instanceof Geometry)){
84  log.error("Could not convert the geometry: it is not of type Geometry. Returning empty string.");
85  return "";
86  }
87 
88  /* Convert to the format: (x1;y1),(x2;y2),...,(xn;yn) */
89  Coordinate[] ca = ((Geometry)o).getCoordinates();
90  StringBuilder s = new StringBuilder();
91  for(int i = 0; i < ca.length-1; i++){
92  s.append("(");
93  s.append(ca[i].x);
94  s.append(";");
95  s.append(ca[i].y);
96  s.append("),");
97  }
98  s.append("(");
99  s.append(ca[ca.length - 1].x);
100  s.append(";");
101  s.append(ca[ca.length - 1].y);
102  s.append(")");
103 
104  return s.toString();
105  }
106 
107 }