Skip to content

Add org.locationtech.jts.geom.Point from Hibernate.spacial operator #201

@jhorber95

Description

@jhorber95

Working with geo-spatial values is an important thing in some projects, so, I want to contribute with an operator =incircle=(lat,lon,radiusMeter), here a simple implementation, but would be nice to add it to project base:

return new RSQLCustomPredicate<>(
            new ComparisonOperator("=incircle=", Arity.nary(3)),
            String.class,
            input -> {
                // input.getArguments() returns the value after the operator, e.g. "4.123,-74.456,500"
                CriteriaBuilder cb = input.getCriteriaBuilder();

                List<Object> values = input.getArguments();
                if (values.size() != 3) {
                    throw new IllegalArgumentException(
                        "=incircle= operator requires 3 comma-separated values: lat,lon,radiusMeters"
                    );
                }
                String[] parts = values.get(0).toString().split(",");

                double lat = Double.parseDouble(parts[0].trim());
                double lon = Double.parseDouble(parts[1].trim());
                double radius = Double.parseDouble(parts[2].trim());

                Point center = GEOMETRY_FACTORY.createPoint(new Coordinate(lon, lat));
                center.setSRID(4326); // WGS84 - make sure this matches your column SRID

                Expression<Double> distanceFunction = cb.function(
                    "ST_Distance",
                    Double.class,
                    input.getPath(),
                    cb.literal(center)
                );

                return cb.lessThanOrEqualTo(distanceFunction, radius);
            }
        );

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions