Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[geomesa-users] KNN-Queries

Hi there,

I´ve got another question corresponding to query-writing. I have found no java documentation for Geomesa so far, thats why I have some problems writing queries. Please have a look at the following peace of code.
The code seems to work, but it doesn´t return anything. My aim is to return the closest event to beijing considering the attribute "geom". I found a KNNQuery class, but it´s not working for me, because there are some "magic" parameters I don´t understand (see comments in my code).

Thanks again.

Marcel.

public static void queryGdelt(String[] args) throws Exception {

        SimpleFeatureSource fs = GDELTGeomesaQueryClusterPreparation.getSimpleFeatureSource(args);
 
        GeometryFactory geomFactory = new GeometryFactory();
        // coordinates for beijing
        double[] coordinates = { 116.3974589, 39.9388838 };
        Coordinate coord = new Coordinate(coordinates[0], coordinates[1]);
        Point point = geomFactory.createPoint(coord);
 
        int unknownParameter1 = 1;
        // second parameter vague
        // attribute parameter "geom" missing? 
        NearestNeighbors neighborsPrepare = NearestNeighbors.apply(point, unknownParameter1);
 
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date start = df.parse("1979-01-01");
        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
        Filter timeFilter = ff.after(ff.property("SQLDATE"), ff.literal(start));
 
        // second and third parameter vague
        double unknownParameter2 = 1;
        double unknownParameter3 = 1;
        GeoHashSpiral spiral = GeoHashSpiral.apply(point, unknownParameter2, unknownParameter3);
        // i dont need an extra filter...knn is my spatialfilter.
        // took complete timeline, because the constructor for query needs a filter.
        NearestNeighbors neighbors = KNNQuery.runKNNQuery(fs,
                new Query("gdelt", timeFilter, new String[] { "SQLDATE", "geom" }), spiral, neighborsPrepare);
 
        // size is always 0
        Log.info(neighbors.size());
 
        // print the results
        Iterator<SimpleFeatureWithDistance> sfListWithDist = neighbors.getK().iterator();
        while (sfListWithDist.hasNext()) {
            SimpleFeatureWithDistance sfWithdist = sfListWithDist.next();
            double minimumDistance = sfWithdist.dist();
            int numberOfAttributes = sfWithdist.sf().getAttributeCount();
            String eventOutput = "";
            for (int currentAttributeNumber = 0; currentAttributeNumber < numberOfAttributes; currentAttributeNumber++) {
                Object o = sfWithdist.sf().getAttributes().get(currentAttributeNumber);
                if (o != null) {
                    eventOutput += o.toString() + " ";
                } else {
                    eventOutput += "NULL ";
                }
            }
            Log.info(minimumDistance + " " + eventOutput.trim());
        }
    }


Back to the top