Search in sources :

Example 11 with JsonParser

use of com.revolsys.record.io.format.json.JsonParser in project com.revolsys.open by revolsys.

the class GeoJsonGeometryReader method readPolygon.

private Polygon readPolygon(final boolean cogo) {
    List<LineString> rings = null;
    GeometryFactory factory = this.geometryFactory;
    do {
        final JsonParser parser = this.in;
        final String fieldName = parser.skipToNextAttribute();
        if (GeoJson.COORDINATES.equals(fieldName)) {
            rings = readCoordinatesListList(cogo, true);
        } else if (GeoJson.CRS.equals(fieldName)) {
            factory = readCoordinateSystem();
        }
    } while (this.in.getEvent() != EventType.endObject && this.in.getEvent() != EventType.endDocument);
    int axisCount = 2;
    for (final LineString points : rings) {
        axisCount = Math.max(axisCount, points.getAxisCount());
    }
    factory = factory.convertAxisCount(axisCount);
    return factory.polygon(rings);
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) LineString(com.revolsys.geometry.model.LineString) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) JsonParser(com.revolsys.record.io.format.json.JsonParser)

Example 12 with JsonParser

use of com.revolsys.record.io.format.json.JsonParser in project com.revolsys.open by revolsys.

the class ArcGisRestServerFeatureReader method getNext.

@SuppressWarnings("resource")
@Override
protected Record getNext() throws NoSuchElementException {
    int previousRecordOffset = this.currentRecordId;
    final int maxRetries = 3;
    for (int retry = 0; retry < maxRetries; retry++) {
        if (this.closed) {
            throw new NoSuchElementException();
        } else {
            JsonParser parser = this.parser;
            if (this.recordCount < this.queryLimit) {
                if (parser == null) {
                    parser = newParser();
                }
                if (!parser.skipToNextObjectInArray()) {
                    if (this.pageByObjectId) {
                        parser = newParser();
                        if (!parser.skipToNextObjectInArray()) {
                            throw new NoSuchElementException();
                        }
                    } else if (this.supportsPaging) {
                        if (this.pageRecordCount == this.pageSize) {
                            parser = newParser();
                            if (!parser.skipToNextObjectInArray()) {
                                throw new NoSuchElementException();
                            }
                        }
                    } else {
                        throw new NoSuchElementException();
                    }
                }
            } else {
                throw new NoSuchElementException();
            }
            if (this.closed) {
                throw new NoSuchElementException();
            } else {
                try {
                    if (parser.isEvent(EventType.endArray, EventType.endDocument)) {
                        throw new NoSuchElementException();
                    }
                    final MapEx recordMap = this.parser.getMap();
                    final Record record = this.recordFacory.newRecord(this.recordDefinition);
                    record.setState(RecordState.INITIALIZING);
                    final MapEx fieldValues = recordMap.getValue("attributes");
                    final int recordId = fieldValues.getInteger(this.idFieldName, -1);
                    this.currentRecordId = recordId;
                    if (this.pageByObjectId) {
                        if (this.currentRecordId == -1) {
                            throw new NoSuchElementException();
                        }
                    }
                    record.setValues(fieldValues);
                    if (this.geometryConverter != null) {
                        final MapEx geometryProperties = recordMap.getValue("geometry");
                        if (Property.hasValue(geometryProperties)) {
                            final Geometry geometry = this.geometryConverter.apply(this.geometryFactory, geometryProperties);
                            record.setGeometryValue(geometry);
                        }
                    }
                    if (parser.hasNext()) {
                        final EventType nextEvent = parser.next();
                        if (nextEvent == EventType.endArray || nextEvent == EventType.endDocument) {
                            this.parser = null;
                        }
                    } else {
                        this.parser = null;
                    }
                    record.setState(RecordState.PERSISTED);
                    this.pageRecordCount++;
                    this.recordCount++;
                    return record;
                } catch (final NoSuchElementException e) {
                    throw e;
                } catch (final Throwable e) {
                    if (retry + 1 == maxRetries) {
                        throw new RuntimeException("Unable to read: " + getPathName(), e);
                    }
                    if (this.pageByObjectId) {
                        if (this.currentRecordId == previousRecordOffset) {
                            if (retry > 1) {
                                Logs.error(this, "Unable to read record: " + getPathName() + " " + this.idFieldName + "=" + (this.currentRecordId + 1));
                                this.currentRecordId++;
                                previousRecordOffset = this.currentRecordId;
                            }
                        } else {
                            Logs.error(this, "Unable to read record: " + getPathName() + " " + this.idFieldName + "=" + (this.currentRecordId + 1));
                            this.currentRecordId++;
                        }
                    } else {
                        close();
                        Exceptions.throwUncheckedException(e);
                    }
                }
            }
        }
    }
    throw new RuntimeException("Unable to read: " + getPathName());
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) MapEx(com.revolsys.collection.map.MapEx) EventType(com.revolsys.record.io.format.json.JsonParser.EventType) Record(com.revolsys.record.Record) Point(com.revolsys.geometry.model.Point) NoSuchElementException(java.util.NoSuchElementException) JsonParser(com.revolsys.record.io.format.json.JsonParser)

Example 13 with JsonParser

use of com.revolsys.record.io.format.json.JsonParser in project com.revolsys.open by revolsys.

the class ArcGisRestServerFeatureIterator method getNext.

@SuppressWarnings("resource")
@Override
protected Record getNext() throws NoSuchElementException {
    int previousRecordOffset = this.currentRecordId;
    final int maxRetries = 3;
    for (int retry = 0; retry < maxRetries; retry++) {
        if (this.closed) {
            throw new NoSuchElementException();
        } else {
            JsonParser parser = this.parser;
            if (this.recordCount < this.queryLimit) {
                if (parser == null) {
                    parser = newParser();
                }
                if (!parser.skipToNextObjectInArray()) {
                    if (this.pageByObjectId) {
                        parser = newParser();
                        if (!parser.skipToNextObjectInArray()) {
                            throw new NoSuchElementException();
                        }
                    } else if (this.supportsPaging) {
                        if (this.pageRecordCount == this.pageSize) {
                            parser = newParser();
                            if (!parser.skipToNextObjectInArray()) {
                                throw new NoSuchElementException();
                            }
                        }
                    } else {
                        throw new NoSuchElementException();
                    }
                }
            } else {
                throw new NoSuchElementException();
            }
            if (this.closed) {
                throw new NoSuchElementException();
            } else {
                try {
                    if (parser.isEvent(EventType.endArray, EventType.endDocument)) {
                        throw new NoSuchElementException();
                    }
                    final MapEx recordMap = this.parser.getMap();
                    final Record record = this.recordFacory.newRecord(this.recordDefinition);
                    record.setState(RecordState.INITIALIZING);
                    final MapEx fieldValues = recordMap.getValue("attributes");
                    this.currentRecordId = fieldValues.getInteger(this.idFieldName, -1);
                    if (this.pageByObjectId) {
                        if (this.currentRecordId == -1) {
                            throw new NoSuchElementException();
                        }
                    }
                    record.setValues(fieldValues);
                    if (this.geometryConverter != null) {
                        final MapEx geometryProperties = recordMap.getValue("geometry");
                        if (Property.hasValue(geometryProperties)) {
                            final Geometry geometry = this.geometryConverter.apply(this.geometryFactory, geometryProperties);
                            record.setGeometryValue(geometry);
                        }
                    }
                    if (parser.hasNext()) {
                        final EventType nextEvent = parser.next();
                        if (nextEvent == EventType.endArray || nextEvent == EventType.endDocument) {
                            this.parser = null;
                        }
                    } else {
                        this.parser = null;
                    }
                    record.setState(RecordState.PERSISTED);
                    this.pageRecordCount++;
                    this.recordCount++;
                    return record;
                } catch (final NoSuchElementException e) {
                    throw e;
                } catch (final Throwable e) {
                    if (retry + 1 == maxRetries) {
                        throw new RuntimeException("Unable to read: " + getPathName(), e);
                    }
                    if (this.pageByObjectId) {
                        if (this.currentRecordId == previousRecordOffset) {
                            if (retry > 1) {
                                Logs.error(this, "Unable to read record: " + getPathName() + " " + this.idFieldName + "=" + (this.currentRecordId + 1));
                                this.currentRecordId++;
                                previousRecordOffset = this.currentRecordId;
                            }
                        } else {
                            Logs.error(this, "Unable to read record: " + getPathName() + " " + this.idFieldName + "=" + (this.currentRecordId + 1));
                            this.currentRecordId++;
                        }
                    } else {
                        close();
                        Exceptions.throwUncheckedException(e);
                    }
                }
            }
        }
    }
    throw new RuntimeException("Unable to read: " + getPathName());
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) MapEx(com.revolsys.collection.map.MapEx) EventType(com.revolsys.record.io.format.json.JsonParser.EventType) Record(com.revolsys.record.Record) Point(com.revolsys.geometry.model.Point) NoSuchElementException(java.util.NoSuchElementException) JsonParser(com.revolsys.record.io.format.json.JsonParser)

Aggregations

JsonParser (com.revolsys.record.io.format.json.JsonParser)13 Point (com.revolsys.geometry.model.Point)10 LineString (com.revolsys.geometry.model.LineString)9 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)8 Geometry (com.revolsys.geometry.model.Geometry)3 EventType (com.revolsys.record.io.format.json.JsonParser.EventType)3 NoSuchElementException (java.util.NoSuchElementException)3 MapEx (com.revolsys.collection.map.MapEx)2 Record (com.revolsys.record.Record)2 ArrayList (java.util.ArrayList)2 Polygon (com.revolsys.geometry.model.Polygon)1 LineStringDouble (com.revolsys.geometry.model.impl.LineStringDouble)1 List (java.util.List)1