Quantcast
Channel: Question and Answer » postgresql
Viewing all articles
Browse latest Browse all 1138

Export PostGIS line to AutoCAD with graphical direction information

$
0
0

Some time ago I managed to export from PostgreSQL to AutoCAD DXF format a line (LINESTRING) using ogr2ogr program with a PostgreSQL Query, and it works (I can visualise it by the QuantumGIS).

Now I really need to, in some way, export graphical information about the direction of that line, like little arrows on the line.

First I tried with the OGR Style Feature “Symbol”, trying to include a query field OGR_STYLE with the content similar to SYMBOL(c:#00FF00,id:"points.sym-45,ogr-sym-7",a:X.xx). But I realised that the DXF driver does not understand and draw this features.

So then I changed radically my idea and now I am trying to export, apart from the line, another DXF file with little triangle polygons simulating the direction of the line, the little arrows.

  • First I get all the points of the line by PostGIS ST_DumpPoints function
  • Then I get every point with its next point with WINDOW PostgreSQL method.
  • Then I get the direction from the point to its next point by PostGIS ST_Azimuth function
  • And finally, to draw the little arrow triangle polygon, I need to get the 2 other points from the final one of the pair, locating them to X distance back from the original, with the direction obtained multiplied to -1 and +X and -X (respectively for each of the 2 new points).
  • Then I would be able to make the little triangles with that 3 points by PostGIS ST_MakePolygon function

Here it is the idea of Query that I would use with the ogr2ogr command -sql parameter, like ogr2ogr -f DXF line_directions.dxf "PG:host=192.168.1.* user=**** password=**** dbname=****" -sql "THE QUERY"

SELECT ST_MakePolygon(ST_MakeLine(ARRAY[point1, point2, point3])) AS the_geom
FROM (
    SELECT *, next_point AS point1, ST_Project(next_point, 10, ((orientation + 1) * (-1))) AS point2, ST_Project(next_point, 10, ((orientation - 1) * (-1))) AS point3
    FROM (
        SELECT *, ST_Distance(point, next_point) AS distance, ST_Azimuth(point, next_point) AS orientation
        FROM (
            SELECT geom_id, path[1] AS path, geom AS point, lead(geom) OVER (PARTITION BY geom_id ORDER BY path ASC) AS next_point
            FROM (
                SELECT a.geom_id, (ST_DumpPoints(a.geom)).* 
                FROM (
                    SELECT id AS geom_id, line AS geom 
                    FROM my_lines_table
                    WHERE line IS NOT NULL
                ) AS a
            ) b
            ORDER BY path ASC
        ) c
        WHERE next_point IS NOT NULL
    ) d
) e;

I know that in PostGIS 2.0 there is the ST_Project function that would help me to do it but I am working with the PostGIS 1.5.3 and I can’t upgrade it.

Any help? Any radically diferent ideas? Thank you very much:)


Viewing all articles
Browse latest Browse all 1138

Trending Articles