Giter VIP home page Giter VIP logo

Comments (6)

ojabri1 avatar ojabri1 commented on June 12, 2024 1

Hello, I have the same request, it will be very helpful to have this feature :)
@jpd-defra for now I am using this to compute it :

data = [
  ("a", "LINESTRING (0 0, 1 1, 1 0)", "POINT (0.5 0.5)"),
  ("b", "LINESTRING (0 0, 1 0, 2 1, 2 2)", "POINT (1 1)"),
]
df = spark_session.createDataFrame(data, ["id", "line_wkt", "point_wkt"])

df = df.withColumn("line", expr("ST_GeomFromWKT(line_wkt)")).withColumn(
  "point", expr("ST_GeomFromWKT(point_wkt)")
)

df = df.withColumn("closest_point", expr("ST_ClosestPoint(line, point)"))
df = df.withColumn("line_vertices", expr("ST_DumpPoints(line)"))

df = df.withColumn(
  "vertex_distances",
  expr(
      "transform(line_vertices, vertex -> ST_Distance(vertex, closest_point))"
  ),
)

df = df.withColumn(
  "closest_vertex_index",
  expr("array_position(vertex_distances, array_min(vertex_distances)) - 1"),
)

df = df.withColumn(
    "line_locate_point",
    when(
        col("closest_vertex_index") == 0,
        expr(
            "ST_Distance(line_vertices[closest_vertex_index + 1], closest_point) / ST_Length(line)"
        ),
    ).otherwise(
        expr(
            "ST_Length(ST_LineFromMultiPoint(ST_Collect(slice(line_vertices, 1, closest_vertex_index+1)))) + ST_Distance(line_vertices[closest_vertex_index + 1], closest_point) / ST_Length(line)"
        )
    ),
)

from sedona.

jiayuasu avatar jiayuasu commented on June 12, 2024

This function can be easily implemented by

    public static double lineLocatePoint(Geometry geom, Geometry point) {
        double length = geom.getLength();
        LengthIndexedLine indexedLine = new LengthIndexedLine(geom);
        return indexedLine.indexOf(point.getCoordinate()) / length;
    }

If you are interested, please refer to the implementation of ST_LineInterpolatePoint: https://github.com/apache/sedona/blob/master/common/src/main/java/org/apache/sedona/common/Functions.java#L635

Then create your PR to Sedona

from sedona.

jpd-defra avatar jpd-defra commented on June 12, 2024

Thanks @jiayuasu. Have never written any Java before, but if its just a case of dropping your suggested code into the Functions.java file and submitting the PR, then I could do that.

from sedona.

jiayuasu avatar jiayuasu commented on June 12, 2024

Thanks. In fact, it is more than that. We need to add more test cases and wrappers.

from sedona.

jiayuasu avatar jiayuasu commented on June 12, 2024

@jpd-defra @ojabri1 Hi folks, Sedona 1.5.1 has been released. ST_LineLocatePoint is included! See: https://sedona.apache.org/1.5.1/api/sql/Function/#st_linelocatepoint

Please give it a try!

from sedona.

jpd-defra avatar jpd-defra commented on June 12, 2024

@jiayuasu - thanks for the heads up. I spun up a Databricks cluster with 1.5.1 but found I was getting a REDACTED_LOCAL_PART type error when trying to read any parquets I had made prior to the upgrade. Not quite sure what is going on, but given a tight deadline for some work, have had to drop back to 1.5.0 for now.

from sedona.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.