kiwi-postgis  Version 0.1.0.0
PolyshapeReader Class Reference
Collaboration diagram for PolyshapeReader:

Classes

class  XReader
 from Apache 2.0 licensed: https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java More...
 

Public Member Functions

String getFormatName ()
 
Geometry read (Object value) throws IOException, ParseException
 
Geometry readIfSupported (Object value)
 
final Geometry read (Reader r) throws ParseException, IOException
 

Protected Member Functions

Geometry readPolygon (XReader reader) throws IOException
 

Package Attributes

final GeometryFactory shpFactory =new GeometryFactory()
 

Detailed Description

Member Function Documentation

◆ getFormatName()

String getFormatName ( )
45  {
46  return "PolyShape";
47  }

◆ read() [1/2]

Geometry read ( Object  value) throws IOException, ParseException

Referenced by PolyshapeDatatype.read(), and PolyshapeReader.readIfSupported().

50  {
51  return read(new StringReader(value.toString().trim()));
52  }
Geometry read(Object value)
Definition: PolyshapeReader.java:50

◆ read() [2/2]

final Geometry read ( Reader  r) throws ParseException, IOException

References PolyshapeReader.XReader.isDone(), PolyshapeReader.XReader.isEvent(), PolyshapeWriter.KEY_ARG_END, PolyshapeWriter.KEY_ARG_START, PolyshapeWriter.KEY_CIRCLE, PolyshapeWriter.KEY_LINE, PolyshapeWriter.KEY_MULTIPOINT, PolyshapeWriter.KEY_POINT, PolyshapeWriter.KEY_POLYGON, PolyshapeWriter.KEY_SEPERATOR, PolyshapeReader.XReader.peek(), PolyshapeReader.XReader.readDouble(), PolyshapeReader.XReader.readKey(), PolyshapeReader.XReader.readLat(), PolyshapeReader.XReader.readLng(), PolyshapeReader.XReader.readPoints(), and PolyshapeReader.readPolygon().

72  {
73  XReader reader = new XReader(r, shpFactory);
74  Double arg = null;
75  GeometryFactory fac=new GeometryFactory();
76  Geometry lastShape = null;
77  List<Geometry> shapes = null;
78  while(!reader.isDone()) {
79  char event = reader.readKey();
80  if(event<'0' || event > '9') {
81  if(event == PolyshapeWriter.KEY_SEPERATOR) {
82  continue; // read the next key
83  }
84  throw new ParseException("expecting a shape key. not '"+event+"'", -1);
85  }
86 
87  if(lastShape!=null) {
88  if(shapes==null) {
89  shapes = new ArrayList<Geometry>();
90  }
91  shapes.add(lastShape);
92  }
93  arg = null;
94 
95  if(reader.peek()==PolyshapeWriter.KEY_ARG_START) {
96  reader.readKey(); // skip the key
97  arg = reader.readDouble();
98  if(reader.readKey()!=PolyshapeWriter.KEY_ARG_END) {
99  throw new ParseException("expecting an argument end", -1);
100  }
101  }
102  if(reader.isEvent()) {
103  throw new ParseException("Invalid input. Event should be followed by data", -1);
104  }
105 
106  switch(event) {
107  case PolyshapeWriter.KEY_POINT: {
108  lastShape = shpFactory.createPoint(new Coordinate(reader.readLat(),reader.readLng()));
109  break;
110  }
111  case PolyshapeWriter.KEY_LINE: {
112 
113  //reader.readPoints(builder)
114  lastShape=fac.createLineString(reader.readPoints());
115  //GeometryFactory.LineStringBuilder lineBuilder = shpFactory.lineString();
116 
117 
118  //if(arg!=null) {
119  //lineBuilder.buffer(shpFactory.normDist(arg));
120  //}
121  //lastShape = lineBuilder.build();
122  break;
123  }
124  /*case PolyshapeWriter.KEY_BOX: {
125 
126  double lat1 = reader.readLat();
127  double lon1 = reader.readLng();
128  double lat2= reader.readLat();
129  double lon2= reader.readLng();
130  lastShape=new Envelope(lat1, lon1, lat2, lon2).;
131 
132  break;
133  }*/
134  case PolyshapeWriter.KEY_MULTIPOINT : {
135  lastShape=fac.createMultiPoint(reader.readPoints());
136  break;
137  }
138  case PolyshapeWriter.KEY_CIRCLE : {
139  if(arg==null) {
140  throw new IllegalArgumentException("the input should have a radius argument");
141  }
142  GeometricShapeFactory fac2=new GeometricShapeFactory();
143  fac2.setCentre(new Coordinate(reader.readLat(),reader.readLng()));
144  fac2.setWidth(arg.doubleValue());
145  lastShape=fac2.createCircle();
146  /*Polygon circle=fac2.createCircle();
147  circle.
148  fac.createLinearRing(coordinates)
149  lastShape = shpFactory.circle(shpFactory.normX(reader.readLat()), shpFactory.normY(reader.readLng()),
150  shpFactory.normDist(arg.doubleValue()));*/
151  break;
152  }
153  case PolyshapeWriter.KEY_POLYGON: {
154  lastShape = readPolygon(reader);
155  break;
156  }
157  default: {
158  throw new ParseException("unhandled key: "+event, -1);
159  }
160  }
161  }
162 
163  if(shapes!=null) {
164  if(lastShape!=null) {
165  shapes.add(lastShape);
166  }
167  if(!shapes.isEmpty() && shapes.get(0)!=null) {
168  if(shapes.get(0) instanceof Point) {
169  return fac.createMultiPoint(GeometryFactory.toPointArray(shapes));
170  }else if(shapes.get(0) instanceof LineString) {
171  return fac.createMultiLineString(GeometryFactory.toLineStringArray(shapes));
172  }else if(shapes.get(0) instanceof Polygon) {
173  return fac.createMultiPolygon(GeometryFactory.toPolygonArray(shapes));
174  }
175  }
176  }
177  return lastShape;
178  }
final GeometryFactory shpFactory
Definition: PolyshapeReader.java:42
Geometry readPolygon(XReader reader)
Definition: PolyshapeReader.java:180

◆ readIfSupported()

Geometry readIfSupported ( Object  value)

References PolyshapeReader.read().

54  {
55  String v = value.toString().trim();
56  char first = v.charAt(0);
57  if(first >= '0' && first <= '9') {
58  try {
59  return read(new StringReader(v));
60  } catch (ParseException e) {
61  } catch (IOException e) {
62  }
63  }
64  return null;
65  }
Geometry read(Object value)
Definition: PolyshapeReader.java:50

◆ readPolygon()

Geometry readPolygon ( XReader  reader) throws IOException
protected

Referenced by PolyshapeReader.read().

180  {
181  //Geometry.PolygonBuilder polyBuilder = shpFactory.polygon();
182  GeometryFactory fac=new GeometryFactory();
183  Polygon poly=fac.createPolygon(reader.readPoints());
184  //reader.readPoints(polyBuilder);
185 
186  /*if(!reader.isDone() && reader.peek()==PolyshapeWriter.KEY_ARG_START) {
187  List<LinearRing> list = new ArrayList<LinearRing>();
188  while(reader.isEvent() && reader.peek()==PolyshapeWriter.KEY_ARG_START) {
189  reader.readKey(); // eat the event;
190  reader.readPoints(polyBuilder.hole()).endHole();
191  }
192  }
193 
194  return polyBuilder.build();*/
195  return poly;
196  }

Member Data Documentation

◆ shpFactory

final GeometryFactory shpFactory =new GeometryFactory()
package