[Cartoweb-users] show geometric object with mapserver ?

Oliver Christen oliver.christen at camptocamp.com
Tue May 23 03:35:04 EDT 2006


See behind an exemple (simplified) of plugin code to get some coordinate in 
a DB and display a point accordingly:
There are 3 parts and 4 files.

CLIENT SIDE (ClientYourPluingName.php)

<?php
class ClientYourPluingName extends ClientPlugin
                       implements ServerCaller {

    private $vehiculeCoords;

    public function initialize() {

        $this->vehiculeCoords = array();
        // get coord from db
        $db = $this->getDb();

        $sql = 'SELECT * FROM "your_table_with_values" WHERE 
any_condition_you_want';

        $result = $db->query($sql);
        if (DB::isError($result)) {
            throw new CartoclientException($result->getMessage());
        }
        $row = NULL;
        while ($result->fetchInto($row, DB_FETCHMODE_ASSOC)) {
            $this->vehiculeCoords['x'] = $row['name_of_the_x_coord_column'];
            $this->vehiculeCoordsY['y'] = 
$row['name_of_the_y_coord_column'];
        }
    }

    private function getDb() {

        if (is_null($this->db)) {
            $dsn = $this->getConfig()->databaseDsn;
            print "dsn= ".$dsn."<br>";

            if (!$dsn) {
                throw new CartoclientException('Search database DSN not 
found');
            }

            $this->db = DB::connect($dsn);

            //check for db connection error, throw exception if needed
            Utils::checkDbError();

        }
        return $this->db;
    }

    /**
     * @see ServerCaller::buildRequest()
     */
    public function buildRequest() {
        // we call for a new shape only if we have a x and y
        if ($this->vehiculeCoords['x'] != '' and $this->vehiculeCoords['y'] 
!= '') {

            $newElement = new Point($this->vehiculeCoords['x'], 
$this->$this->vehiculeCoords['y']);
            $styledShape = new StyledShape();
            $styledShape->shape = $newElement;

            $shapeStyle = new ShapeStyle();

            $shapeStyle->symbol = $this->type;
            $shapeStyle->size = 20;
            $shapeStyle->color->setFromRGB(51, 0, 204);
            $shapeStyle->outlineColor->setFromRGB(0, 0, 0);

            $styledShape->shapeStyle = $shapeStyle;

            $YourPluingNameRequest = new YourPluingNameRequest();
            $YourPluingNameRequest->shapes = array($styledShape);

            return $YourPluingNameRequest;
        }
    }


    /**
     * @see ServerCaller::initializeResult()
     */
    public function initializeResult($YourPluingNameResult) {}


    /**
     * @see ServerCaller::handleResult()
     */
    public function handleResult($YourPluingNameResult) {}

?>

COMMON (YourPluingName.php)
<?php

    class YourPluingNameRequest extends Serializable {

      /**
       * Styled shapes to be drawn
       * @var array
       */
      public $shapes;

      /**
       * @see Serializable::unserialize()
       */
      public function unserialize($struct) {
        $this->shapes = self::unserializeObjectMap($struct, 'shapes',
                                                   'StyledShape');
      }
    }

    /**
     * Result
     * @package Plugins
     */
    class YourPluingNameResult {}

?>
COMMON (yourPluingName.wsdl.inc)

      <!-- outline -->

      <complexType name="Color">
        <all>
          <element name="r" type="xsd:int"/>
          <element name="g" type="xsd:int"/>
          <element name="b" type="xsd:int"/>
        </all>
      </complexType>

      <complexType name="ShapeStyle">
        <all>
          <element name="symbol" type="xsd:int"/>
          <element name="size" type="xsd:int"/>
          <element name="color" type="types:Color"/>
          <element name="outlineColor" type="types:Color"/>
          <element name="backgroundColor" type="types:Color"/>
          <element name="transparency" type="xsd:int"/>
        </all>
      </complexType>

      <complexType name="LabelStyle">
        <all>
          <element name="font" type="xsd:int"/>
          <element name="size" type="xsd:int"/>
          <element name="color" type="types:Color"/>
          <element name="outlineColor" type="types:Color"/>
          <element name="backgroundColor" type="types:Color"/>
        </all>
      </complexType>

      <complexType name="StyledShape">
        <all>
          <element name="shapeStyle" type="types:ShapeStyle"/>
          <element name="labelStyle" type="types:LabelStyle"/>
          <element name="shape" type="types:Shape"/>
          <element name="label" type="xsd:string"/>
        </all>
      </complexType>

      <complexType name="ArrayOfStyledShape">
        <complexContent>
          <restriction base="enc11:Array">
            <attribute ref="enc11:arrayType" 
wsdl:arrayType="types:StyledShape[]"/>
          </restriction>
        </complexContent>
      </complexType>

      <complexType name="OutlineRequest">
        <all>
          <element name="className" type="xsd:string"/>
          <element name="shapes" type="types:ArrayOfStyledShape"/>
          <element name="maskMode" type="xsd:boolean"/>
        </all>
      </complexType>

      <complexType name="OutlineResult">
        <all>
          <element name="className" type="xsd:string"/>
          <element name="area" type="xsd:double"/>
        </all>
      </complexType>

SERVER (ServerYourPluingName.php)

<?php
class ServerYourPluingName extends ClientResponderAdapter {

    /**
     * @var Logger
     */
    private $log;

    /**
     * @var objet
     */
    private $shapes;


    /**
     * Constructor
     */
    function __construct() {
        parent::__construct();
        $this->log =& LoggerManager::getLogger(__CLASS__);
    }


    /**
     * @see ClientResponder::initializeRequest()
     */
    function initializeRequest($requ) {
        if ($requ)
            $this->shapes = $requ->shapes;
    }


    /**
     * Result is set in initializeRequest but Outline must be called
     * in handlePreDrawing
     * @see ClientResponder::handlePreDrawing()
     */
    function handlePreDrawing($requ) {
        if ($requ) {
            $pluginManager = $this->serverContext->getPluginManager();
            if (empty($pluginManager->outline))
                throw new CartoserverException("outline plugin not loaded, "
                                               . "and needed to draw the new 
element");
            $pluginManager->outline->draw($this->shapes);
        }
    }

}

?>



> mapserver alone or mapserver+cartoweb?
>
> In Cartoweb you need to create a new plugin that will make a request on 
> the DB
> and then generate some simple geometry (a point i suppose) at the given
> coordinate.
> I can show you some code exemple monday when im back to work and have my
> projects files under the hand.
>
> If only mapserver, I suppose you will have to code some phpmapscript 
> functions,
> but im no expert in that.
>
> Regards
> Oliver
>
>> Hi,
>> I use postgresql-postgis in a project for fleet vehicle management,i need 
>> to
>> have the last postion of vehicle
>> I have create a table with :
>>
>> CREATE TABLE vehicle (id_vehicle serial, time timestamp, x numeric, y
>> numeric,  speed text, PRIMARY KEY(id_vehicle ,time));
>>
>> what can i do to show last position with mapserver ?
>>
>>
>> --
>> "Qui allume sa bougie à la mienne reçoit de la lumière sans me plonger 
>> dans
>> l'obscurité."
>>                                                  ' Thomas Jefferson'
>>
>
>
>
> _______________________________________________
> Cartoweb-users mailing list
> Cartoweb-users at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/cartoweb-users
> 



More information about the Cartoweb-users mailing list