Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6937067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:18:59+00:00 2026-05-27T12:18:59+00:00

I am struggling with an SQL command issued from my python script. Here is

  • 0

I am struggling with an SQL command issued from my python script. Here is what I have tried so far, the first example works fine but the rest do not.

#working SQL = "SELECT ST_Distance(ST_Transform(ST_GeomFromText(%s, 4326),27700),ST_Transform(ST_GeomFromText(%s, 4326),27700));"
#newPointSQL = "SELECT ST_ClosestPoint(ST_GeomFromText(%s),ST_GeomFromText(%s));"
#newPointSQL = "SELECT ST_As_Text(ST_ClosestPoint(ST_GeomFromText(%s), ST_GeomFromText(%s)));"
#newPointSQL = "SELECT ST_AsText(ST_ClosestPoint(ST_GeomFromEWKT(%s), ST_GeomFromText(%s)));"
#newPointSQL = "SELECT ST_AsText(ST_Line_Interpolate_Point(ST_GeomFromText(%s),ST_Line_Locate_Point(ST_GeomFromText(%s),ST_GeomFromText(%s))));"

newPointData = (correctionPathLine,pointToCorrect) - ( MULTILINESTRING((-3.16427109855617 55.9273798550064,-3.16462372283029 55.9273883602162)), POINT(-3.164667 55.92739)) 

My data is picked up ok because the first sql is successfull when executed. The problem is when I use the ST_ClosestPoint function.
Can anyone notice a misuse anywhere? Am I using the ST_ClosetsPoint in a wrong way?
In the last example, I did modify my data (in case someone notices) to run it but it still would not execute.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:19:00+00:00Added an answer on May 27, 2026 at 12:19 pm

    I don’t know with what kind of geometries you are dealing with, but I had the same trouble before with MultiLineStrings, I realized that when a MultiLinestring can’t be merged, the function ST_Line_Locate_Point doesn’t work.(you can know if a MultiLineString can’t be merged using the ST_LineMerge function) I’ve made a pl/pgSQL function based in an old maillist but I added some performance tweaks, It only works with MultiLineStrings and LineStrings (but can be easily modified to work with Polygons). First it checks if the geometry only has 1 dimension, if it has, you can use the old ST_Line_Interpolate_Point and ST_Line_Locate_Point combination, if not, then you have to do the same for each LineString in the MultiLineString. Also I’ve added a ST_LineMerge for pre 1.5 compatibility :

    CREATE OR REPLACE FUNCTION ST_MultiLine_Nearest_Point(amultiline geometry,apoint geometry) 
      RETURNS geometry AS
    $BODY$
    DECLARE
        mindistance float8;
        adistance float8;
        nearestlinestring geometry;
        nearestpoint geometry;
        simplifiedline geometry;
        line geometry;
    BEGIN
            simplifiedline:=ST_LineMerge(amultiline);
            IF ST_NumGeometries(simplifiedline) <= 1 THEN
                nearestpoint:=ST_Line_Interpolate_Point(simplifiedline, ST_Line_Locate_Point(simplifiedline,apoint) );
                RETURN nearestpoint;
          END IF;
    --      *Change your mindistance according to your projection, it should be stupidly big*
            mindistance := 100000; 
            FOR line IN SELECT (ST_Dump(simplifiedline)).geom as geom LOOP
                    adistance:=ST_Distance(apoint,line);
                IF adistance < mindistance THEN
                    mindistance:=adistance;
                    nearestlinestring:=line; 
                END IF;
            END LOOP;
            RETURN ST_Line_Interpolate_Point(nearestlinestring,ST_Line_Locate_Point(nearestlinestring,apoint));
        END;
        $BODY$
          LANGUAGE 'plpgsql' IMMUTABLE STRICT; 
    

    UPDATE:

    As noted by @Nicklas Avén ST_Closest_Point() should work, ST_Closest_Point was added in 1.5 .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling to find the right terminology here, but if you have jQuery object...
I have the following SQL query: select AuditStatusId from dbo.ABC_AuditStatus where coalesce(AuditFrequency, 0) <>
I'm struggling with some SQL/php syntax issues here. I'm trying to write two functions.
I am struggling to convert this sql query SELECT feature.*, rolefeature.roleId, rolefeature.permission FROM feature
I'm really struggling with this query. I have 4 tables (http://oberto.co.nz/db-sql.png): Invoice_Payement, Invoice, Client
I'm struggling with correct procedure for executing SQL query. Basically, I have text field
I'm struggling with an exception using linq-to-sql Concat() I've got 2 tables. The first
i have a simple SQL query, but im struggling to replicate in LINQ select
I have found out that my SQL 2008 R2 database is really struggling with
I am struggling with an SQL time comparison on two fields that have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.