MATSIM
PointFeatureFactory.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2012 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.utils.gis;
21 
22 import java.util.Collections;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25 
26 import org.geotools.api.feature.simple.SimpleFeature;
27 import org.geotools.api.feature.simple.SimpleFeatureType;
28 import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
29 import org.geotools.feature.simple.SimpleFeatureBuilder;
30 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
31 import org.locationtech.jts.geom.Coordinate;
32 import org.locationtech.jts.geom.GeometryFactory;
33 import org.locationtech.jts.geom.Point;
34 import org.matsim.api.core.v01.Coord;
35 
39 public class PointFeatureFactory {
40 
41  private final Map<String, Class<?>> attributes;
42  private final SimpleFeatureType featureType;
43  private final GeometryFactory fac = new GeometryFactory();
44  private final SimpleFeatureBuilder builder;
45 
46  private PointFeatureFactory(Map<String, Class<?>> attributes, SimpleFeatureType featureType) {
47  this.attributes = attributes;
48  this.featureType = featureType;
49  this.builder = new SimpleFeatureBuilder(this.featureType);
50  }
51 
52  public SimpleFeatureType getFeatureType() {
53  return featureType;
54  }
55 
56  public SimpleFeature createPoint(final Coordinate coordinate) {
57  return this.createPoint(coordinate, Collections.<String, Object> emptyMap(), null);
58  }
59 
60  public SimpleFeature createPoint(final Coordinate coordinate, final Map<String, Object> attributeValues, final String id) {
61  Point p = this.fac.createPoint(coordinate);
62 
63  this.builder.add(p);
64  for (String name : this.attributes.keySet()) {
65  Object value = attributeValues.get(name);
66  this.builder.add(value);
67  }
68 
69  return this.builder.buildFeature(id);
70  }
71 
72  public SimpleFeature createPoint(final Coordinate coordinate, final Object[] attributeValues, final String id) {
73  Point p = this.fac.createPoint(coordinate);
74  return this.createPoint(p, attributeValues, id);
75  }
76 
77  public SimpleFeature createPoint(final Point point, final Object[] attributeValues, final String id) {
78  this.builder.add(point);
79  for (int i = 0; i < attributeValues.length; i++) {
80  Object value = attributeValues[i];
81  this.builder.add(value);
82  }
83  return this.builder.buildFeature(id);
84  }
85 
86  public SimpleFeature createPoint(final Coord coordinate, final Object[] attributeValues, final String id) {
87  Point p = this.fac.createPoint(new Coordinate(coordinate.getX(), coordinate.getY()));
88 
89  this.builder.add(p);
90  for (int i = 0; i < attributeValues.length; i++) {
91  Object value = attributeValues[i];
92  this.builder.add(value);
93  }
94  return this.builder.buildFeature(id);
95  }
96 
97  public static class Builder {
98  private CoordinateReferenceSystem crs = null;
99  private String name = "";
100  private Map<String, Class<?>> attributes = new LinkedHashMap<String, Class<?>>();
101 
102  public Builder() {
103  }
104 
105  public Builder setCrs(CoordinateReferenceSystem crs) {
106  this.crs = crs;
107  return this;
108  }
109 
110  public Builder addAttribute(final String name, final Class<?> type) {
111  this.attributes.put(name, type);
112  return this;
113  }
114 
115  public Builder setName(final String name) {
116  this.name = name;
117  return this;
118  }
119 
121  SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
122  b.setName(this.name);
123  b.setCRS(this.crs);
124  b.add("the_geom", Point.class);
125  for (Map.Entry<String, Class<?>> attr : this.attributes.entrySet()) {
126  b.add(attr.getKey(), attr.getValue());
127  }
128  SimpleFeatureType featureType = b.buildFeatureType();
129  return new PointFeatureFactory(this.attributes, featureType);
130  }
131  }
132 
133 }
SimpleFeature createPoint(final Coordinate coordinate, final Map< String, Object > attributeValues, final String id)
Builder addAttribute(final String name, final Class<?> type)
SimpleFeature createPoint(final Coord coordinate, final Object[] attributeValues, final String id)
PointFeatureFactory(Map< String, Class<?>> attributes, SimpleFeatureType featureType)
SimpleFeature createPoint(final Point point, final Object[] attributeValues, final String id)
SimpleFeature createPoint(final Coordinate coordinate)
SimpleFeature createPoint(final Coordinate coordinate, final Object[] attributeValues, final String id)