sensiasoft / lib-ogc

Library for manipulating various data models and service models defined in OGC standards
Mozilla Public License 2.0
5 stars 2 forks source link

SML JSON bindings do not handle Vector location properly #8

Open earocorn opened 2 hours ago

earocorn commented 2 hours ago

When using SMLJsonBindings to write the position object in JSON, there is no handler for Vector or DataRecords that contain Vector as location.

The current state of writePosition()

protected void writePosition(JsonWriter writer, Serializable bean) throws IOException
    {
        writer.name("position");

        if (bean instanceof Point)
        {
            geojsonBindings.writePoint(writer, (Point)bean);
        }
        else if (bean instanceof Pose)
        {
            geoposeBindings.writePose(writer, (Pose)bean);
        }
    }

This can cause errors when requesting data in SML JSON format.

My temporary solution is to write location as a GeoJSON Point, but I believe this is not the optimal solution.

protected void writePosition(JsonWriter writer, Serializable bean) throws IOException
    {
        writer.name("position");

        if (bean instanceof Point)
        {
            geojsonBindings.writePoint(writer, (Point)bean);
        }
        else if (bean instanceof Pose)
        {
            geoposeBindings.writePose(writer, (Pose)bean);
        }
        else if (bean instanceof Vector)
        {
            // Vector deprecated, use Point format for position
            writeVectorAsPoint(writer, (Vector)bean);
        }
        else if (bean instanceof DataRecord)
        {
            // DataRecord including location and orientation, write location Vector as Point
            Vector location = (Vector) ((DataRecord) bean).getComponent("location");
            writeVectorAsPoint(writer, location);
        }
    }

    protected void writeVectorAsPoint(JsonWriter writer, Vector bean) throws IOException {
        int dims = bean.getNumCoordinates();
        double[] coords = new double[dims];

        for(int i = 0; i < dims; i++)
        {
            coords[i] = bean.getCoordinateList().get(i).getData().getDoubleValue();
        }

        Point point = gmlFactory.newPoint();
        point.setSrsDimension(dims);
        point.setPos(coords);

        geojsonBindings.writePoint(writer, point);
    }