Search in sources :

Example 1 with ShapeWriter

use of org.locationtech.spatial4j.io.ShapeWriter in project lucene-solr by apache.

the class GeoFieldUpdater method create.

@Override
public DocTransformer create(String display, SolrParams params, SolrQueryRequest req) {
    String fname = params.get("f", display);
    if (fname.startsWith("[") && fname.endsWith("]")) {
        fname = display.substring(1, display.length() - 1);
    }
    SchemaField sf = req.getSchema().getFieldOrNull(fname);
    if (sf == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, this.getClass().getSimpleName() + " using unknown field: " + fname);
    }
    if (!(sf.getType() instanceof AbstractSpatialFieldType)) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoTransformer requested non-spatial field: " + fname + " (" + sf.getType().getClass().getSimpleName() + ")");
    }
    final GeoFieldUpdater updater = new GeoFieldUpdater();
    updater.field = fname;
    updater.display = display;
    updater.display_error = display + "_error";
    ValueSource shapes = null;
    AbstractSpatialFieldType<?> sdv = (AbstractSpatialFieldType<?>) sf.getType();
    SpatialStrategy strategy = sdv.getStrategy(fname);
    if (strategy instanceof CompositeSpatialStrategy) {
        shapes = ((CompositeSpatialStrategy) strategy).getGeometryStrategy().makeShapeValueSource();
    } else if (strategy instanceof SerializedDVStrategy) {
        shapes = ((SerializedDVStrategy) strategy).makeShapeValueSource();
    }
    String writerName = params.get("w", "GeoJSON");
    updater.formats = strategy.getSpatialContext().getFormats();
    updater.writer = updater.formats.getWriter(writerName);
    if (updater.writer == null) {
        StringBuilder str = new StringBuilder();
        str.append("Unknown Spatial Writer: ").append(writerName);
        str.append(" [");
        for (ShapeWriter w : updater.formats.getWriters()) {
            str.append(w.getFormatName()).append(' ');
        }
        str.append("]");
        throw new SolrException(ErrorCode.BAD_REQUEST, str.toString());
    }
    QueryResponseWriter qw = req.getCore().getQueryResponseWriter(req);
    updater.isJSON = (qw.getClass() == JSONResponseWriter.class) && (updater.writer instanceof GeoJSONWriter);
    // Using ValueSource
    if (shapes != null) {
        // we don't really need the qparser... just so we can reuse valueSource
        QParser parser = new QParser(null, null, params, req) {

            @Override
            public Query parse() throws SyntaxError {
                return new MatchAllDocsQuery();
            }
        };
        return new ValueSourceAugmenter(display, parser, shapes) {

            @Override
            protected void setValue(SolrDocument doc, Object val) {
                updater.setValue(doc, val);
            }
        };
    }
    // Using the raw stored values
    return new DocTransformer() {

        @Override
        public void transform(SolrDocument doc, int docid, float score) throws IOException {
            Object val = doc.remove(updater.field);
            if (val != null) {
                updater.setValue(doc, val);
            }
        }

        @Override
        public String getName() {
            return updater.display;
        }

        @Override
        public String[] getExtraRequestFields() {
            return new String[] { updater.field };
        }
    };
}
Also used : CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) ShapeWriter(org.locationtech.spatial4j.io.ShapeWriter) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) SerializedDVStrategy(org.apache.lucene.spatial.serialized.SerializedDVStrategy) SchemaField(org.apache.solr.schema.SchemaField) SolrDocument(org.apache.solr.common.SolrDocument) ValueSource(org.apache.lucene.queries.function.ValueSource) QParser(org.apache.solr.search.QParser) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) GeoJSONWriter(org.locationtech.spatial4j.io.GeoJSONWriter) SolrException(org.apache.solr.common.SolrException)

Example 2 with ShapeWriter

use of org.locationtech.spatial4j.io.ShapeWriter in project gtfs-realtime-validator by CUTR-at-USF.

the class VehicleValidatorTest method testStopAndTripShapeBounds.

/**
 * Used as part of:
 * E028 - Vehicle position outside agency coverage area
 * E029 - Vehicle position outside trip shape buffer
 *
 * @throws IOException
 */
@Test
public void testStopAndTripShapeBounds() throws IOException {
    Rectangle stopBoundingBox = bullRunnerGtfsMetadata.getStopBoundingBox();
    Rectangle stopBoundingBoxWithBuffer = bullRunnerGtfsMetadata.getStopBoundingBoxWithBuffer();
    Rectangle shapeBoundingBox = bullRunnerGtfsMetadata.getShapeBoundingBox();
    Rectangle shapeBoundingBoxWithBuffer = bullRunnerGtfsMetadata.getShapeBoundingBoxWithBuffer();
    ShapeFactory sf = GEO.getShapeFactory();
    Point p;
    SpatialRelation spatialRelation;
    /**
     * Point is inside GTFS stops.txt bounding box
     */
    // USF Campus in Tampa, FL
    p = sf.pointXY(-82.4139, 28.0587);
    spatialRelation = stopBoundingBox.relate(p);
    assertEquals(SpatialRelation.CONTAINS, spatialRelation);
    spatialRelation = stopBoundingBoxWithBuffer.relate(p);
    assertEquals(SpatialRelation.CONTAINS, spatialRelation);
    spatialRelation = shapeBoundingBox.relate(p);
    assertEquals(SpatialRelation.CONTAINS, spatialRelation);
    spatialRelation = shapeBoundingBoxWithBuffer.relate(p);
    assertEquals(SpatialRelation.CONTAINS, spatialRelation);
    /**
     * Point is outside of GTFS stops.txt bounding box
     */
    // Downtown Tampa, FL
    p = sf.pointXY(-82.4655826, 27.9482837);
    spatialRelation = stopBoundingBox.relate(p);
    assertNotEquals(SpatialRelation.CONTAINS, spatialRelation);
    spatialRelation = stopBoundingBoxWithBuffer.relate(p);
    assertNotEquals(SpatialRelation.CONTAINS, spatialRelation);
    spatialRelation = shapeBoundingBox.relate(p);
    assertNotEquals(SpatialRelation.CONTAINS, spatialRelation);
    spatialRelation = shapeBoundingBoxWithBuffer.relate(p);
    assertNotEquals(SpatialRelation.CONTAINS, spatialRelation);
    // NYC
    p = sf.pointXY(-74.0059, 40.7128);
    spatialRelation = stopBoundingBox.relate(p);
    assertNotEquals(SpatialRelation.CONTAINS, spatialRelation);
    spatialRelation = stopBoundingBoxWithBuffer.relate(p);
    assertNotEquals(SpatialRelation.CONTAINS, spatialRelation);
    spatialRelation = shapeBoundingBox.relate(p);
    assertNotEquals(SpatialRelation.CONTAINS, spatialRelation);
    spatialRelation = shapeBoundingBoxWithBuffer.relate(p);
    assertNotEquals(SpatialRelation.CONTAINS, spatialRelation);
    /**
     * Point is inside of USF Bull Runner Route A (trip_id=2) polygon (buffer surrounding shapes.txt shape)
     */
    String tripId = "2";
    Shape routeABuffered = bullRunnerGtfsMetadata.getBufferedTripShape(tripId);
    // USF Marshall Center
    p = sf.pointXY(-82.4131679534912, 28.064065878608385);
    spatialRelation = routeABuffered.relate(p);
    assertEquals(SpatialRelation.CONTAINS, spatialRelation);
    /**
     * Point is outside of USF Bull Runner Route A polygon (buffer surrounding shapes.txt shape)
     */
    // University Mall
    p = sf.pointXY(-82.43475437164307, 28.057438520876673);
    spatialRelation = routeABuffered.relate(p);
    assertNotEquals(SpatialRelation.CONTAINS, spatialRelation);
    /**
     * Test GeoJSON output - for troubleshooting and visualizing using http://geojson.io/
     */
    PrintWriter writer = new PrintWriter(System.out);
    ShapeFactory jtsSf = JtsSpatialContext.GEO.getShapeFactory();
    ShapeWriter shpWriter = jtsSf.getSpatialContext().getFormats().getWriter(ShapeIO.GeoJSON);
    writer.append("USF Bull Runner stops.txt bounding box --------------\n");
    shpWriter.write(writer, stopBoundingBox);
    writer.append("\nUSF Bull Runner stops.txt bounding box with buffer --------------\n");
    shpWriter.write(writer, stopBoundingBoxWithBuffer);
    writer.flush();
    writer.append("\nUSF Bull Runner shapes.txt bounding box --------------\n");
    shpWriter.write(writer, shapeBoundingBox);
    writer.append("\nUSF Bull Runner shapes.txt bounding box with buffer --------------\n");
    shpWriter.write(writer, shapeBoundingBoxWithBuffer);
    writer.flush();
    writer.append("\nUSF Bull Runner Route A (trip_id=2) trip shape output --------------\n");
    shpWriter.write(writer, routeABuffered);
    writer.flush();
    Rectangle gtfsBoundingBox = gtfsDataMetadata.getStopBoundingBox();
    writer.append("\ntestagency.zip bounding box output --------------\n");
    shpWriter.write(writer, gtfsBoundingBox);
    writer.flush();
    clearAndInitRequiredFeedFields();
}
Also used : ShapeWriter(org.locationtech.spatial4j.io.ShapeWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) FeedMessageTest(edu.usf.cutr.gtfsrtvalidator.lib.test.FeedMessageTest)

Aggregations

ShapeWriter (org.locationtech.spatial4j.io.ShapeWriter)2 FeedMessageTest (edu.usf.cutr.gtfsrtvalidator.lib.test.FeedMessageTest)1 PrintWriter (java.io.PrintWriter)1 ValueSource (org.apache.lucene.queries.function.ValueSource)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)1 CompositeSpatialStrategy (org.apache.lucene.spatial.composite.CompositeSpatialStrategy)1 SerializedDVStrategy (org.apache.lucene.spatial.serialized.SerializedDVStrategy)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrException (org.apache.solr.common.SolrException)1 JSONResponseWriter (org.apache.solr.response.JSONResponseWriter)1 QueryResponseWriter (org.apache.solr.response.QueryResponseWriter)1 AbstractSpatialFieldType (org.apache.solr.schema.AbstractSpatialFieldType)1 SchemaField (org.apache.solr.schema.SchemaField)1 QParser (org.apache.solr.search.QParser)1 Test (org.junit.Test)1 GeoJSONWriter (org.locationtech.spatial4j.io.GeoJSONWriter)1