Friday, December 9, 2011

How-To find the closest point of a MultiLineString from a Point in PostGIS

I had the same trouble before with MultiLineStrings, I realized that when a MultiLinestring can't be merged, all functions like ST_ClosestPoint and 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;

Wednesday, September 7, 2011

Java HMTL-like Imagemap

How many of you ever wanted to use an imagemap in Java (awt - swingx)?, well all you need to do is download the code from github and you need to make an image for each element of the imagemap and put the region or element in the place that you want it to appear, It's easier if you open the whole image in photoshop or gimp, and just start to create layers for each region, and save each layer in a different file, like this:



A tooltip shows you in which region the pointer is, you just need to add a click event listener to get the id of the region that was clicked:

Saturday, May 7, 2011

Why I don't like unity?

First of all, lets think why someone use linux, maybe most of us started being script kiddies, others in school had to use it in some courses, others in theirs work. But the ones that use it everyday, like it because its as cool or as practical as you want. For example I use default configurations of the desktop environment in my work's linuxes and for my personal machines I use a tweaked version with all the candy-eye you can get. For many of us, adopting gnome 3 was the natural evolution of ubuntu, but for Mark Shuttleworth it seems wrong. Instead he went for unity, actually unity its only Mark's favorite compiz configuration. Another issue is the unilateral changes he is making to the layout just to mention a few, Why they moved the window management icons to the left????
Lets take for example my configuration, its a 10.04 ubuntu with compiz and awn.





Only with a few extra tweaks it does everything gnome 3 and unity does. So??? we will have to wait a little to see how it ends.