/** * Parses the next number in the stream. Numbers with exponents are handled. <tt>NaN</tt> values * are handled correctly, and the case of the "NaN" token is not significant. * * @param tokenizer tokenizer over a stream of text in Well-known Text format. The next token * must be a number. * @return the next number in the stream * @throws ParseException if the next token is not a valid number * @throws IOException if an I/O error occurs */ private double getNextNumber() throws IOException, ParseException { int type = tokenizer.nextToken(); switch (type) { case StreamTokenizer.TT_WORD: { if (tokenizer.sval.equalsIgnoreCase(NAN_SYMBOL)) { return Double.NaN; } else { try { return Double.parseDouble(tokenizer.sval); } catch (NumberFormatException ex) { throw new ParseException("Invalid number: " + tokenizer.sval); } } } } parseError("number"); return 0.0; }
private Geometry readCompoundCurve() throws IOException, ParseException { int numGeom = dis.readInt(); List<LineString> geoms = new ArrayList<>(); for (int i = 0; i < numGeom; i++) { Geometry g = readGeometry(); if (!(g instanceof LineString)) throw new ParseException(INVALID_GEOM_TYPE_MSG + "CompoundCurve"); geoms.add((LineString) g); } return factory.createCurvedGeometry(geoms); }
/** * Reads a Well-Known Text representation of a {@link Geometry} from a {@link Reader}. * * @param reader a Reader which will return a <Geometry Tagged Text> string (see the OpenGIS * Simple Features Specification) * @return a <code>Geometry</code> read from <code>reader</code> * @throws ParseException if a parsing problem occurs */ public Geometry read(Reader reader) throws ParseException { tokenizer = new StreamTokenizer(reader); // set tokenizer to NOT parse numbers tokenizer.resetSyntax(); tokenizer.wordChars('a', 'z'); tokenizer.wordChars('A', 'Z'); tokenizer.wordChars(128 + 32, 255); tokenizer.wordChars('0', '9'); tokenizer.wordChars('-', '-'); tokenizer.wordChars('+', '+'); tokenizer.wordChars('.', '.'); tokenizer.whitespaceChars(0, ' '); tokenizer.commentChar('#'); try { return readGeometryTaggedText(); } catch (IOException e) { throw new ParseException(e.toString()); } }
private MultiPoint readMultiPoint() throws IOException, ParseException { int numGeom = dis.readInt(); Point[] geoms = new Point[numGeom]; for (int i = 0; i < numGeom; i++) { Geometry g = readGeometry(); if (!(g instanceof Point)) throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiPoint"); geoms[i] = (Point) g; } return factory.createMultiPoint(geoms); }
private MultiPoint readMultiPoint() throws IOException, ParseException { int numGeom = dis.readInt(); Point[] geoms = new Point[numGeom]; for (int i = 0; i < numGeom; i++) { Geometry g = readGeometry(); if (!(g instanceof Point)) throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiPoint"); geoms[i] = (Point) g; } return factory.createMultiPoint(geoms); }
private MultiLineString readMultiLineString() throws IOException, ParseException { int numGeom = dis.readInt(); LineString[] geoms = new LineString[numGeom]; for (int i = 0; i < numGeom; i++) { Geometry g = readGeometry(); if (!(g instanceof LineString)) throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiLineString"); geoms[i] = (LineString) g; } return factory.createMultiLineString(geoms); }
private MultiLineString readMultiLineString() throws IOException, ParseException { int numGeom = dis.readInt(); LineString[] geoms = new LineString[numGeom]; for (int i = 0; i < numGeom; i++) { Geometry g = readGeometry(); if (!(g instanceof LineString)) throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiLineString"); geoms[i] = (LineString) g; } return factory.createMultiLineString(geoms); }
private MultiPolygon readMultiPolygon() throws IOException, ParseException { int numGeom = dis.readInt(); Polygon[] geoms = new Polygon[numGeom]; for (int i = 0; i < numGeom; i++) { Geometry g = readGeometry(); if (!(g instanceof Polygon)) throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiPolygon"); geoms[i] = (Polygon) g; } return factory.createMultiPolygon(geoms); }
private MultiPolygon readMultiPolygon() throws IOException, ParseException { int numGeom = dis.readInt(); Polygon[] geoms = new Polygon[numGeom]; for (int i = 0; i < numGeom; i++) { Geometry g = readGeometry(); if (!(g instanceof Polygon)) throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiPolygon"); geoms[i] = (Polygon) g; } return factory.createMultiPolygon(geoms); }
/** * Throws a formatted ParseException for the current token. * * @param expected a description of what was expected * @throws ParseException * @throws AssertionFailedException if an invalid token is encountered */ private void parseError(String expected) throws ParseException { // throws Asserts for tokens that should never be seen if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) Assert.shouldNeverReachHere("Unexpected NUMBER token"); if (tokenizer.ttype == StreamTokenizer.TT_EOL) Assert.shouldNeverReachHere("Unexpected EOL token"); String tokenStr = tokenString(); throw new ParseException("Expected " + expected + " but found " + tokenStr); }
/** * Creates a <code>LineString</code> using the next token in the stream. * * @return * @throws IOException * @throws ParseException */ private LineString readCircularStringText() throws IOException, ParseException { List<Coordinate> coordinates = getCoordinateList(true); if (coordinates.size() == 0) { return geometryFactory.createCurvedGeometry( new LiteCoordinateSequence(new Coordinate[0])); } else if (coordinates.size() < 3) { throw new ParseException("A CIRCULARSTRING must contain at least 3 control points"); } else { double[] controlPoints = toControlPoints(coordinates); return geometryFactory.createCurvedGeometry(2, controlPoints); } }
return readLineStringText(4, 1); throw new ParseException("Unknown geometry type: " + type);
return readGeometryCollection(); throw new ParseException("Unknown WKB type " + geometryType);
throw new ParseException("Unknown WKB type " + geometryType);
/** * Creates a formatted ParseException reporting that the current token * was unexpected. * * @param msg a description of what was expected * @throws AssertionFailedException if an invalid token is encountered */ private static ParseException parseErrorWithLine(StreamTokenizer tokenizer, String msg) { return new ParseException(msg + " (line " + tokenizer.lineno() + ")"); }
public Geometry read(byte[] bytes) throws ParseException { try { ByteArrayInputStream in = new ByteArrayInputStream(bytes); DataInput input = new DataInputStream(in); return read(input); } catch (IOException e) { throw new ParseException("Error reading TWKB geometry.", e); } }
private MultiLineString readMultiLineString() throws IOException, ParseException { int numGeom = dis.readInt(); LineString[] geoms = new LineString[numGeom]; for (int i = 0; i < numGeom; i++) { Geometry g = readGeometry(); if (! (g instanceof LineString)) throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiLineString"); geoms[i] = (LineString) g; } return factory.createMultiLineString(geoms); }
private MultiPolygon readMultiPolygon() throws IOException, ParseException { int numGeom = dis.readInt(); Polygon[] geoms = new Polygon[numGeom]; for (int i = 0; i < numGeom; i++) { Geometry g = readGeometry(); if (! (g instanceof Polygon)) throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiPolygon"); geoms[i] = (Polygon) g; } return factory.createMultiPolygon(geoms); }
@Override public String marshal(Polygon the_geom) throws ParseException { if (the_geom != null) { WKTWriter wktWriter = new WKTWriter(); if (the_geom.getSRID() == 0) the_geom.setSRID(4326); return wktWriter.write(the_geom); } else { throw new ParseException("Polygon obj is null."); } } }
@Override public String marshal(G the_geom) throws ParseException { if (the_geom != null) { WKTWriter wktWriter = new WKTWriter(); if (the_geom.getSRID() == 0) the_geom.setSRID(4326); return wktWriter.write(the_geom); } else { throw new ParseException("Geometry obj is null."); } } }