Java Code Examples for org.geotools.filter.text.ecql.ECQL#toFilter()

The following examples show how to use org.geotools.filter.text.ecql.ECQL#toFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticFilterTest.java    From elasticgeo with GNU General Public License v3.0 7 votes vote down vote up
@Test
public void testGeoShapeIntersectsFilter() throws CQLException {
    Intersects filter = (Intersects) ECQL.toFilter("INTERSECTS(\"geom\", LINESTRING(0 0,1.1 1.1))");
    List<List<Double>> coords = new ArrayList<>();
    coords.add(ImmutableList.of(0.,0.));
    coords.add(ImmutableList.of(1.1,1.1));
    Map<String,Object> expected = ImmutableMap.of("bool", 
            ImmutableMap.of("must", MATCH_ALL, "filter", ImmutableMap.of("geo_shape", 
                    ImmutableMap.of("geom", ImmutableMap.of("shape", 
                            ImmutableMap.of("coordinates", coords, "type", "LineString"),
                            "relation", "INTERSECTS")))));

    builder.visit(filter, null);
    assertTrue(builder.createCapabilities().fullySupports(filter));
    // TODO: Why doesn't equality check on objects work here
    assertEquals(expected.toString(), builder.getQueryBuilder().toString());
}
 
Example 2
Source File: FilterServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
/** @inheritDoc */
public Filter parseFilter(String filter) throws GeomajasException {
	if (null == filter || filter.length() == 0) {
		return createTrueFilter();
	}
	try {
		if (idReplacer.shouldParse(filter)) {
			return idReplacer.parse(filter);
		} else {
			return ECQL.toFilter(filter, FF);
		}
	} catch (CQLException e) {
		// ECQL should be a superset of CQL, but there are apparently extra key words like "id"
		// fall back to CQL for backwards compatibility
		log.warn("Filter not parsable by ECQL, falling back to CQL", e);
		try {
			return CQL.toFilter(filter, FF);
		} catch (CQLException ce) {
			throw new GeomajasException(ce, ExceptionCode.FILTER_PARSE_PROBLEM, filter);
		}
	}
}
 
Example 3
Source File: ElasticFilterTest.java    From elasticgeo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGeoPolygonFilter() throws CQLException {
    Intersects filter = (Intersects) ECQL.toFilter("INTERSECTS(\"geo_point\", POLYGON((0 0, 0 1.1, 1.1 1.1, 1.1 0, 0 0)))");
    List<List<Double>> points = ImmutableList.of(
            ImmutableList.of(0.,0.),
            ImmutableList.of(0.,1.1),
            ImmutableList.of(1.1,1.1),
            ImmutableList.of(1.1,0.),
            ImmutableList.of(0.,0.));
    Map<String,Object> expected = ImmutableMap.of("bool", 
            ImmutableMap.of("must", MATCH_ALL, "filter", ImmutableMap.of("geo_polygon", 
                    ImmutableMap.of("geo_point", ImmutableMap.of("points", points)))));

    builder.visit(filter, null);
    assertTrue(builder.createCapabilities().fullySupports(filter));
    assertEquals(expected, builder.getQueryBuilder());
}
 
Example 4
Source File: GeoWaveVectorRecordReader.java    From mrgeo with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(InputSplit split, TaskAttemptContext context) throws IOException,
    InterruptedException
{
  delegateReader.initialize(split, context);
  // No need to initialize the delegateReader since it was already done in the
  // GeoWaveInputFormat.createRecordReader method
  strCqlFilter = context.getConfiguration().get(CQL_FILTER);
  if (strCqlFilter != null && !strCqlFilter.isEmpty())
  {
    try
    {
      log.info("Creating the CQL filter");
      cqlFilter = ECQL.toFilter(strCqlFilter);
      log.info("Done creating the CQL filter");
    }
    catch (CQLException e)
    {
      throw new IOException("Unable to instantiate CQL filter for: " + strCqlFilter, e);
    }
  }
}
 
Example 5
Source File: GeoWaveGTQueryTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void executeCQLQueryTest() throws IOException, CQLException {
    System.out.println("Executing query, expecting to match two points...");

    final Filter cqlFilter = ECQL.toFilter("BBOX(geometry,-77.6167,38.6833,-76.6,38.9200) and locationName like 'W%'");

    final QueryOptions queryOptions = new QueryOptions(ADAPTER, INDEX);
    final CQLQuery cqlQuery = new CQLQuery(null, cqlFilter, ADAPTER);

    try (final CloseableIterator<SimpleFeature> iterator = dataStore.query(queryOptions, cqlQuery)) {
        int count = 0;
        while (iterator.hasNext()) {
            System.out.println("Query match: " + iterator.next().getID());
            count++;
        }
        System.out.println("executeCQLQueryTest count: " + count);
        // Should match "Washington Monument" and "White House"
        assertEquals(2, count);
    }
}
 
Example 6
Source File: ElasticFilterTest.java    From elasticgeo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testCompoundFilter() throws CQLException {
    Filter filter = ECQL.toFilter("time > \"1970-01-01\" and INTERSECTS(\"geom\", LINESTRING(0 0,1.1 1.1))");
    List<List<Double>> coords = new ArrayList<>();
    coords.add(ImmutableList.of(0.,0.));
    coords.add(ImmutableList.of(1.1,1.1));
    Map<String,Object> expected = ImmutableMap.of("bool", 
            ImmutableMap.of("must", ImmutableList.of(ImmutableMap.of("range", ImmutableMap.of("time", ImmutableMap.of("gt", "1970-01-01"))),
                    ImmutableMap.of("bool", ImmutableMap.of("must", MATCH_ALL, 
                            "filter", ImmutableMap.of("geo_shape", 
                                    ImmutableMap.of("geom", ImmutableMap.of("shape", 
                                            ImmutableMap.of("coordinates", coords, "type", "LineString"),
                                            "relation", "INTERSECTS"))))))));

    builder.encode(filter);
    assertTrue(builder.createCapabilities().fullySupports(filter));
    assertEquals(expected.toString(), builder.getQueryBuilder().toString());
}
 
Example 7
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testLike() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    final Query query = new Query(
            "GeoWaveFeatureReaderTest",
            ECQL.toFilter("pid like '" + pids.get(
                    0).substring(
                    0,
                    1) + "%'"),
            new String[] {
                "geometry",
                "pid"
            });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertEquals(1, count);
}
 
Example 8
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testFidFilterQuery() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    final String fidsString = fids.stream().collect(Collectors.joining("','", "'", "'"));
    final Filter filter = ECQL.toFilter("IN (" + fidsString + ")");
    final Query query = new Query(
            "GeoWaveFeatureReaderTest",
            filter,
            new String[] {
                "geometry",
                "pid"
            });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertTrue(count == fids.size());
}
 
Example 9
Source File: OptimalCQLQuery.java    From geowave with Apache License 2.0 5 votes vote down vote up
public static QueryConstraints createOptimalQuery(
    final String cql,
    final GeotoolsFeatureDataAdapter adapter,
    final CompareOperation geoCompareOp,
    final Index index,
    final BasicQueryByClass baseQuery) throws CQLException {
  final Filter cqlFilter = ECQL.toFilter(cql);
  return createOptimalQuery(cqlFilter, adapter, geoCompareOp, index, baseQuery);
}
 
Example 10
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveFeature() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    final Query query = new Query(
            "GeoWaveFeatureReaderTest",
            ECQL.toFilter("pid like '" + pids.get(
                    0).substring(
                    0,
                    1) + "%'"),
            new String[] {
                "geometry",
                "pid"
            });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertEquals(1, count);

    // Remove
    final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
        dataStore.getFeatureWriter(type.getTypeName(), Transaction.AUTO_COMMIT);
    try {
        while (writer.hasNext()) {
            writer.next();
            writer.remove();
        }
    } finally {
        writer.close();
    }

    // Re-query
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader2 =
            dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int recount = 0;
    while (reader2.hasNext()) {
        reader2.next();
        recount++;
    }
    assertEquals(0, recount);
}
 
Example 11
Source File: ClientSideCQLQuery.java    From geowave with Apache License 2.0 5 votes vote down vote up
private void getFilter() {
  try {
    filter = ECQL.toFilter(cql);
  } catch (final CQLException e) {
    LOGGER.warn("Unable to retrive filter", e);
  }
}
 
Example 12
Source File: ElasticFilterTest.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCql() throws CQLException {
    Filter filter = ECQL.toFilter("\"object.field\"='value'");
    Map<String,Object> expected = ImmutableMap.of("term", ImmutableMap.of("object.field", "value"));

    builder.encode(filter);
    assertTrue(builder.createCapabilities().fullySupports(filter));
    assertEquals(expected, builder.getQueryBuilder());
}
 
Example 13
Source File: ElasticFilterTest.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDBeyondFilter() throws CQLException {
    Beyond filter = (Beyond) ECQL.toFilter("BEYOND(\"geo_point\", POINT(0 1.1), 1.0, meters)");
    Map<String,Object> expected = ImmutableMap.of("bool", ImmutableMap.of("must_not", ImmutableMap.of("bool", 
            ImmutableMap.of("must", MATCH_ALL, "filter", ImmutableMap.of("geo_distance", ImmutableMap.of("distance", "1.0m",
                    "geo_point", ImmutableList.of(0.,1.1)))))));

    builder.visit(filter, null);
    assertTrue(builder.createCapabilities().fullySupports(filter));
    assertEquals(expected, builder.getQueryBuilder());       
}
 
Example 14
Source File: ElasticFilterTest.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDWithinPolygonFilter() throws CQLException {
    DWithin filter = (DWithin) ECQL.toFilter("DWITHIN(\"geo_point\", POLYGON((0 0, 0 1, 1 1, 1 0, 0 0)), 1.0, meters)");
    Map<String,Object> expected = ImmutableMap.of("bool", 
            ImmutableMap.of("must", MATCH_ALL, "filter", ImmutableMap.of("geo_distance", 
                    ImmutableMap.of("distance", "1.0m",
                            "geo_point", ImmutableList.of(0.5,0.5)))));

    builder.visit(filter, null);
    assertTrue(builder.createCapabilities().fullySupports(filter));
    assertEquals(expected, builder.getQueryBuilder());       
}
 
Example 15
Source File: ElasticFilterTest.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDWithinFilter() throws CQLException {
    DWithin filter = (DWithin) ECQL.toFilter("DWITHIN(\"geo_point\", POINT(0 1.1), 1.0, meters)");
    Map<String,Object> expected = ImmutableMap.of("bool", 
            ImmutableMap.of("must", MATCH_ALL, "filter", ImmutableMap.of("geo_distance", 
                    ImmutableMap.of("distance", "1.0m",
                            "geo_point", ImmutableList.of(0.,1.1)))));

    builder.visit(filter, null);
    assertTrue(builder.createCapabilities().fullySupports(filter));
    assertEquals(expected, builder.getQueryBuilder());       
}
 
Example 16
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws SchemaException, CQLException, Exception {
    setupConf();
    try (final GeoWaveGeoIndexer indexer = new GeoWaveGeoIndexer()) {
        indexer.setConf(conf);
        dataStore = indexer.getGeoToolsDataStore();
        // Clear old data
        indexer.purge(conf);

        type = DataUtilities.createType(
                "GeoWaveFeatureReaderTest",
                "geometry:Geometry:srid=4326,start:Date,end:Date,pop:java.lang.Long,pid:String");

        dataStore.createSchema(type);

        stime = DateUtilities.parseISO("2005-05-15T20:32:56Z");
        etime = DateUtilities.parseISO("2005-05-20T20:32:56Z");

        final Transaction transaction1 = new DefaultTransaction();
        final FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriter(
                type.getTypeName(),
                transaction1);
        assertFalse(writer.hasNext());
        SimpleFeature newFeature = writer.next();
        newFeature.setAttribute(
                "pop",
                Long.valueOf(100));
        newFeature.setAttribute(
                "pid",
                "a" + UUID.randomUUID().toString());
        newFeature.setAttribute(
                "start",
                stime);
        newFeature.setAttribute(
                "end",
                etime);
        newFeature.setAttribute(
                "geometry",
                factory.createPoint(new Coordinate(27.25, 41.25)));
        fids.add(newFeature.getID());
        pids.add(newFeature.getAttribute("pid").toString());
        writer.write();
        newFeature = writer.next();
        newFeature.setAttribute(
                "pop",
                Long.valueOf(101));
        newFeature.setAttribute(
                "pid",
                "b" + UUID.randomUUID().toString());
        newFeature.setAttribute(
                "start",
                etime);
        newFeature.setAttribute(
                "geometry",
                factory.createPoint(new Coordinate(28.25, 41.25)));
        fids.add(newFeature.getID());
        pids.add(newFeature.getAttribute("pid").toString());
        writer.write();
        writer.close();
        transaction1.commit();
        transaction1.close();

        query = new Query(
                "GeoWaveFeatureReaderTest",
                ECQL.toFilter("IN ('" + fids.get(0) + "')"),
                new String[] {
                    "geometry",
                    "pid"
                });
    }
}
 
Example 17
Source File: FilterUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public static Filter getCQLFilter( String expression ) throws CQLException {
    Filter cqlFilter = ECQL.toFilter(expression);
    return cqlFilter;
}
 
Example 18
Source File: CQLFilterOptionProvider.java    From geowave with Apache License 2.0 4 votes vote down vote up
private static Filter asFilter(final String cqlPredicate) throws CQLException {
  return ECQL.toFilter(cqlPredicate);
}
 
Example 19
Source File: AnalyticFeatureTest.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Test
public void testGeometryCreation() throws MismatchedDimensionException,
    NoSuchAuthorityCodeException, FactoryException, CQLException, ParseException {
  final SimpleFeatureType ftype =
      AnalyticFeature.createGeometryFeatureAdapter(
          "centroid",
          new String[] {"extra1"},
          BasicFeatureTypes.DEFAULT_NAMESPACE,
          ClusteringUtils.CLUSTERING_CRS).getFeatureType();
  final GeometryFactory factory = new GeometryFactory();
  SimpleFeature feature =
      AnalyticFeature.createGeometryFeature(
          ftype,
          "b1",
          "123",
          "fred",
          "NA",
          20.30203,
          factory.createPoint(new Coordinate(02.33, 0.23)),
          new String[] {"extra1"},
          new double[] {0.022},
          1,
          1,
          0);
  assertEquals(
      new Coordinate(02.33, 0.23),
      ((Geometry) feature.getDefaultGeometry()).getCoordinate());
  System.out.println(((Geometry) feature.getDefaultGeometry()).getPrecisionModel());
  System.out.println(((Geometry) feature.getDefaultGeometry()).getEnvelope());

  feature =
      AnalyticFeature.createGeometryFeature(
          ftype,
          "b1",
          "123",
          "fred",
          "NA",
          20.30203,
          factory.createPoint(new Coordinate(02.33, 0.23)),
          new String[] {"extra1"},
          new double[] {0.022},
          10,
          1,
          0);

  assertEquals(
      new Coordinate(02.33, 0.23),
      ((Geometry) feature.getDefaultGeometry()).getCoordinate());

  assertEquals(
      "geometry",
      feature.getFeatureType().getGeometryDescriptor().getName().getLocalPart());

  assertEquals(
      new Integer(10),
      feature.getAttribute(ClusterFeatureAttribute.ZOOM_LEVEL.attrName()));

  Filter gtFilter = ECQL.toFilter("BBOX(geometry,2,0,3,1) and level = 10");
  assertTrue(gtFilter.evaluate(feature));
  gtFilter = ECQL.toFilter("BBOX(geometry,2,0,3,1) and level = 9");
  assertFalse(gtFilter.evaluate(feature));
  gtFilter = ECQL.toFilter("BBOX(geometry,2,0,3,1) and batchID = 'b1'");
  assertTrue(gtFilter.evaluate(feature));
}
 
Example 20
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws SchemaException, CQLException, Exception {
  dataStore = createDataStore();
  type =
      DataUtilities.createType(
          "GeoWaveFeatureReaderTest",
          "geometry:Geometry:srid=4326,start:Date,end:Date,pop:java.lang.Long,pid:String");
  ((GeoWaveGTDataStore) dataStore).getIndexStore().addIndex(
      new SpatialIndexBuilder().createIndex());
  ((GeoWaveGTDataStore) dataStore).getIndexStore().addIndex(
      new SpatialTemporalIndexBuilder().createIndex());
  dataStore.createSchema(type);

  stime = DateUtilities.parseISO("2005-05-15T20:32:56Z");
  mtime = DateUtilities.parseISO("2005-05-20T20:32:56Z");
  etime = DateUtilities.parseISO("2005-05-25T20:32:56Z");

  final Transaction transaction1 = new DefaultTransaction();
  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", "a" + UUID.randomUUID().toString());
  newFeature.setAttribute("start", stime);
  newFeature.setAttribute("end", mtime);
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
  fids.add(newFeature.getID());
  pids.add(newFeature.getAttribute("pid").toString());
  writer.write();
  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(101));
  newFeature.setAttribute("pid", "b" + UUID.randomUUID().toString());
  newFeature.setAttribute("start", mtime);
  newFeature.setAttribute("end", etime);
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(28.25, 41.25)));
  fids.add(newFeature.getID());
  pids.add(newFeature.getAttribute("pid").toString());
  writer.write();
  writer.close();
  transaction1.commit();
  transaction1.close();

  query =
      new Query(
          "GeoWaveFeatureReaderTest",
          ECQL.toFilter("IN ('" + fids.get(0) + "')"),
          new String[] {"geometry", "pid"});
}