wiki:SWIGMapScriptNewLayer

Creating a WMS layer, a Polygon layer with Opacity, a Point layer with an inline feature, using SWIG MapScript and MS4W 5.x

<?php

// define variables
define( "MAPFILE", "C:/ms4w/apps/local-demo/local.map" );

// open map
$oMap = new mapObj(MAPFILE);

//force all errors to display
//  comment out the next 2 lines, useful on servers not displaying errors
ini_set('display_errors','On');
error_reporting(E_ALL);

// set image size
$oMap->setsize(400, 300);

// set image format
$oMap->selectoutputformat("png");

//******
// create new polygon layerObj
//******
$oPolyLayer = new layerObj($oMap);
$oPolyLayer->__set("name", "prov_bound");
$oPolyLayer->__set("type", MS_LAYER_POLYGON);
$oPolyLayer->__set("status", MS_ON);
$oPolyLayer->__set("data", "province.shp");
// create new class for poly layer
$oClass = new classObj($oPolyLayer); 
$oClass->__set("name", "Province");
// create new style
$oStyle = new styleObj(); 
// create new colors
$oColor = new colorObj(120,120,120); 
$oOutlineColor = new colorObj(0,0,0); 
$oStyle->__set("color", $oColor);
$oStyle->__set("outlinecolor", $oOutlineColor);
$oClass->insertStyle($oStyle);
$oPolyLayer->SetProjection("init=EPSG:3978");
// set opacity (actually creates a new COMPOSITE object)
$oPolyLayer->setOpacity(40);

//******
// create new WMS layer
//******
$oWMSLayer = new layerObj($oMap);
$oWMSLayer->__set("name", "cities");
$oWMSLayer->__set("type", MS_LAYER_RASTER);
$oWMSLayer->__set("status", MS_ON);
$oWMSLayer->setConnectionType(MS_WMS, NULL);
$oWMSLayer->__set("connection", "https://demo.gatewaygeomatics.com/cgi-bin/wms_gateway?");
// setMetaData() is no longer available, so we must now access the 'metadata' hashTableObj keys and values directly, such as:
$oWMSLayer->metadata->set("wms_name", "popplace");
$oWMSLayer->metadata->set("wms_srs", "EPSG:3978");
$oWMSLayer->metadata->set("wms_server_version", "1.1.1");
$oWMSLayer->metadata->set("wms_format", "image/png");
$oWMSLayer->SetProjection("init=EPSG:3978");

//******
// create new point layerObj
//******
$oPointLayer = new layerObj($oMap);
$oPointLayer->__set("name", "gg-location");
$oPointLayer->__set("type", MS_LAYER_POINT);
$oPointLayer->__set("status", MS_ON);
$oPointLayer->__set("units", "DD");
$oPoint = new pointObj(-64.2576966,44.3596731);
// create new lineObj (lineObj is composed of one or more pointObj instances)
$oline = new lineObj();
$oline->add($oPoint);
// create shapeObj for point
$oShape = new shapeObj();
$oShape->add($oline);
$oPointLayer->addFeature($oShape); 
// create new class for poly layer
$oClass = new classObj($oPointLayer); 
$oClass->__set("name", "GatewayGeo");
// create new style
$oStyle = new styleObj();
// set symbol
$oStyle->setSymbolByName($oMap, "star");
$oStyle->__set("size", 10);
// create new colors
$oColor = new colorObj(255,255,0); 
$oStyle->__set("color", $oColor);
$oClass->insertStyle($oStyle);
$oPointLayer->SetProjection("init=EPSG:4326");

// draw map
$oImage = $oMap->draw();

// save image file
//$file = $oImage->save("C:/ms4w/apps/local-demo/ttt.png",$oMap);

// save mapfile to new file
//$oMap->save("C:/ms4w/apps/local-demo/output.map");

// set header
header("Content-type: image/png");

//send image to stdout, without saving file locally
echo $oImage ->getBytes();

?>
Last modified 3 years ago Last modified on 2021-12-23T14:16:33-04:00
Note: See TracWiki for help on using the wiki.