[Mapserver-users] Need Urgent Help on PHP/MapScript >> ms_newPointObj()

Merlos merlos at tsc.uvigo.es
Tue Jul 20 19:45:41 EDT 2004


Hi,

Try out this nice code. I think it is extrapolable to any kind of shape, just 
adding more lines to the shp object.

Regards. Juan M. Merlos


This is the layer definition.
  LAYER
    GROUP "user"
    NAME "lines"
    METADATA
        "label" "0,0 como la San Miguel"
        "symbol_" "phone.png"
    END #metadata
    TYPE line
    STATUS on
#    FEATURE 
#     POINTS 0.0 0.0 3.0 3.0 4.5 4.0 END
#    END
   CLASS
       NAME "0"
       TEMPLATE "ttt_query.html"
       STYLE
         COLOR 255 0 0 # red
         MAXSIZE 300 # default 50
         SIZE 16
       END #style
   END #class
   TOLERANCE 8 
   TOLERANCEUNITS pixels #pixels|feet|inches|kilometers|meters|miles|dd
  END #layer

<?php

dl('php_mapscript.so');
//dl('php_mapscript_42.dll'); //For WinDoS Users

//Convert pixels to map units got from PHPMapscriptSnippet1
function click2map ($click_x, $click_y) {
    global $map;
    $e= &$map->extent; //Tip for saving typing time
    $x_pct = ($click_x / $map->width);
    $y_pct = 1 - ($click_y / $map->height);
    $x_map = $e->minx + ( ($e->maxx - $e->minx) * $x_pct);
    $y_map = $e->miny + ( ($e->maxy - $e->miny) * $y_pct);

    return array($x_map, $y_map);
}


// --------------- MAIN -----------------------

$map= ms_newMapObj('data/test2.map');

//Avoid register global=on;
import_request_variables("gP", "");

//Convert pixels to map units
$map_pt = click2map($_POST['image_x'],$_POST['image_y'],$map->extent);

//Construct the line
$line = ms_newLineObj();
$line->addXY($map_pt[0],$map_pt[1]);
if (is_array($prev_x)) {
    foreach ($prev_x as $i => $x){
        $line->addXY($x, $prev_y[$i]);
    }
}


$shp = ms_newShapeObj(MS_SHAPE_LINE);
$shp-> add($line);

$layer = $map->getLayerByName('lines');
//$layer->open();
$layer->addFeature($shp);

//Draw the map and add the point
$img = $map->draw();
//$pt->draw($map, $layer, $img, 0 ,'yuhuu');
$url = $img->saveWebImage();

//Now add hidden fields to the form


for($i=0;$i<$line->numpoints;$i++) {
    $pt = $line->point($i);
    $x = $pt->x;
    $y = $pt->y;
    $HTML_HIDDEN.="<input type=\"hidden\" name=\"prev_x[$i]\" value=\"$x\"/>";
    $HTML_HIDDEN.="<input type=\"hidden\" name=\"prev_y[$i]\" value=\"$y\"/>";
}

?>

<html><head><title>Yapig</title></head>
<body>
<form method="post" action="query3.php" >
   <input style="border:thin solid black;"type="image" name="image" 
src="<?php echo$
     <?php echo $HTML_HIDDEN ?>
</form>
<a href="query3.php">Clear</a>
<pre>
<?php
echo "GET\n";
print_r($_GET);
echo "Point:\n";
print_r($pt); ?>
?>
 </body>



On Tuesday 20 July 2004 20:10, Ishrar Hussain wrote:
> Dear Andy and Merlos,
>
> Thank you both so very much, as both of your codes
> helped me a lot.
>
> I'm also looking forward to using LineObj of
> PHP/MapScript. The sequence of code for drawing lines
> on the fly seems a little different then drawing
> points, as the points of a line need to be added one
> by one.
>
> Please help me in this regard, if possible.
> Thanks once again, for everything.
>
> Regards,
> Ishrar
>
> --- ANDY CANFIELD <andy_canfield at hotmail.com> wrote:
> > You can also make a copy of an existing layer on the
> > fly with a new name
> > that way you don't have to build a whole new layer
> > from scratch. That's how
> > I put geocoded points onto a map. I have a
> > construction layer and I just
> > make copies of it as I need new layers that way the
> > only thing I have to
> > change is the color of the symbol. I have the symbol
> > of that layer as a star
> > so they geocode one textfile of addresses red and
> > the next one blue etc so
> > they can look at the stars from different files at
> > the same time. Each set
> > of different colored stars is simply a copy of that
> > construction layer with
> > the symbol color changed.
> >
> > It looks something like this psuedo code:
> >
> > //draw the map with all your mapfile layers
> > $img = $oMap->draw();
> >
> > //get the dummy point layer
> > $oConsLayer = $oMap->getLayerByName("Construction");
> >
> > //make a copy of it
> > $oConsLayer1 = ms_newLayerObj($oMap, $oConsLayer);
> >
> > //give it a name
> > $oConsLayer1->set("name", "Red Points");
> >
> > //get the class object
> > $oClass = $oConsLayer1->getClass(0);
> >
> > //add the color to the map in case it's not there
> > $colorId = $oMap->addColor(255,0,0);
> >
> > //set your new color
> > $oClass->set("color", $colorId);
> >
> > //make a new point and give it coords
> > $m_Point = ms_newPointObj();
> > $m_Point->setXY($SomeX, $SomeY );
> >
> > //draw the point
> > $m_Point->draw($oMap, $oConsLayer1, $ img,  0,
> > $StringToDisplay);
> >
> > I may be forgetting some steps but that should be
> > roughly what you need to
> > do to create a copy of an existing layer with a new
> > color for the symbol.
> >
> > >From: Merlos <merlos at tsc.uvigo.es>
> > >Reply-To: merlos at tsc.uvigo.es
> > >To: Ishrar Hussain <ishrarh at yahoo.com>
> > >C$oConsLayer1C: mapserver-users at lists.gis.umn.edu
> > >Subject: Re: [Mapserver-users] Need Urgent Help on
> >
> > PHP/MapScript  >>
> >
> > >ms_newPointObj()
> > >Date: Tue, 20 Jul 2004 01:34:28 +0200
> > >
> > >After making some tests, I think that if you want
> >
> > to create a new layer on
> >
> > >the fly you have to create using mapscript a new
> >
> > style and a new class as
> >
> > >well. On my tests, I removed the style of the
> >
> > INLINE layer and the result
> >
> > >was
> > >that it dissapeared on the map image, it seems that
> >
> > there is no default
> >
> > >style
> > >and class set.
> > >
> > >  I'm quite newbie, so maybe a phpmascript guru can
> >
> > corroborate my beliefs
> >
> > >  Anywat, why don't you add an empty layer as I
> >
> > did?  That way you will
> >
> > >save
> > >coding time :-)
> > >
> > >Here you have the one I use:
> > >
> > >   LAYER
> > >     NAME "INLINE"
> > >     TYPE POINT
> > >     STATUS ON
> > >    CLASS
> > >        NAME "0"
> > >        STYLE
> > >          COLOR 255 0 0 # red
> > >          MAXSIZE 300 # default 50
> > >          SYMBOL 'phone'
> > >          SIZE 16
> > >        END #style
> > >    END #class
> > >  END #layer
> > >
> > >On Monday 19 July 2004 21:55, Ishrar Hussain wrote:
> > > > Dear Merlos,
> > > >
> > > > Thank you very much for your reply.
> > > >
> > > > Seeing your code, the question I now have is
> >
> > that, if
> >
> > > > the "Inline" layer (as you've mentioned) is not
> > > > present in my map file, and I create a point
> >
> > layer
> >
> > > > with ms_newLayerObj(), then do I need to draw()
> >
> > the
> >
> > > > layer before referring its instance while
> >
> > drawing the
> >
> > > > point?
> > > >
> > > > Looking forward to your reply.
> > > > Thanks again.
> > > >
> > > > Regards,
> > > > Ishrar
> > > >
> > > > --- Merlos <merlos at tsc.uvigo.es> wrote:
> > > > > Try this. It works for me.
> > > > >
> > > > > <?php
> > > > >
> > > > > $my_map= ms_newMapObj('data/test2.map');
> > > > >
> > > > > $pt = ms_newPointObj();
> > > > > $pt-> setXY(5,5);
> > > > > $img = $my_map->draw();
> > > > > //Inline is a POINT layer.
> > > > > $layer = $my_map->getLayerByName('INLINE');
> > > > > $pt->draw($my_map, $layer, $img, 0 ,'yuhuu');
> > > > >
> > > > >
> > > > > $url = $img->saveWebImage();
> > > > >
> > > > > echo "<img src=\"$url\" alt=\"Yuhuu!\" />";
> > > > >
> > > > > ?>
> > > > >
> > > > > Regards. Merlos.
> > > > >
> > > > > On Monday 19 July 2004 17:52, Ishrar Hussain
> >
> > wrote:
> > > > > > Please give me an example code showing how
> >
> > to use
> >
> > > > > > PointObj class, i.e. ms_newPointObj(), to
> > > > >
> > > > > dynamically
> > > > >
> > > > > > draw a point over an already created
> >
> > instance of
> >
> > > > > > MapObj, which have been instantiated with a
> >
> > static
> >
> > > > > > .map file.
> > > > > >
> > > > > > Using Maplab, I have created my web mapping
> > > > > > application, which successfully published my
> >
> > shape
> >
> > > > > > files in 4 layers. The map is now static.
> >
> > And over
> >
> > > > > > this map, I need to draw with PHP/MapScript
> >
> > a new
> >
> > > > > > layer that will hold one dynamic point,
> >
> > whose
> >
> > > > > > coordinates I will be retrieving from a
> >
> > database
> >
> > > > > using
> > > > >
> > > > > > PHP. (I am editing the drawmap.php file in
> >
> > the
> >
> > > > > wrapper
> > > > >
> > > > > > folder for this.)
> > > > > >
> > > > > > Note that, I don't want to have this new
> >
> > layer
> >
> > > > > > definition in my map file, which will make
> >
> > the
> >
> > > > > layer
> > > > >
> > > > > > static. I also cannot use any database
> >
> > connection
> >
> > > > > in
> > > > >
> > > > > > the mapfile (postgis or postgre or mygis).
> >
> > Thus,
> >
> > > > > > PHP/MapScript's PointObj is my only choice.
>
> === message truncated ===
>
>
>
>
> __________________________________
> Do you Yahoo!?
> New and Improved Yahoo! Mail - Send 10MB messages!
> http://promotions.yahoo.com/new_mail
> _______________________________________________
> Mapserver-users mailing list
> Mapserver-users at lists.gis.umn.edu
> http://lists.gis.umn.edu/mailman/listinfo/mapserver-users

-- 
--
http://www.merlos.biz



More information about the mapserver-users mailing list