| 1 | = Creating a WMS layer, a Polygon layer with Opacity, a Point layer with an inline feature, using SWIG !MapScript = |
| 2 | |
| 3 | {{{ |
| 4 | #!php |
| 5 | <?php |
| 6 | |
| 7 | // define variables |
| 8 | define( "MAPFILE", "C:/ms4w/apps/phpmapscriptng-swig/sample.map" ); |
| 9 | |
| 10 | // required SWIG include (contains constants for PHP7) |
| 11 | include("C:/ms4w/apps/phpmapscriptng-swig/include/mapscript.php"); |
| 12 | |
| 13 | // open map |
| 14 | $oMap = new mapObj(MAPFILE); |
| 15 | |
| 16 | //force all errors to display |
| 17 | // comment out the next 2 lines, useful on servers not displaying errors |
| 18 | ini_set('display_errors','On'); |
| 19 | error_reporting(E_ALL); |
| 20 | |
| 21 | // set image size |
| 22 | $oMap->setsize(400, 300); |
| 23 | |
| 24 | // set image format |
| 25 | $oMap->selectoutputformat("png"); |
| 26 | |
| 27 | //****** |
| 28 | // create new polygon layerObj |
| 29 | //****** |
| 30 | $oPolyLayer = new layerObj($oMap); |
| 31 | $oPolyLayer->__set("name", "prov_bound"); |
| 32 | $oPolyLayer->__set("type", MS_LAYER_POLYGON); |
| 33 | $oPolyLayer->__set("status", MS_ON); |
| 34 | $oPolyLayer->__set("data", "province.shp"); |
| 35 | // create new class for poly layer |
| 36 | $oClass = new classObj($oPolyLayer); |
| 37 | $oClass->__set("name", "Province"); |
| 38 | // create new style |
| 39 | $oStyle = new styleObj(); |
| 40 | // create new colors |
| 41 | $oColor = new colorObj(120,120,120); |
| 42 | $oOutlineColor = new colorObj(0,0,0); |
| 43 | $oStyle->__set("color", $oColor); |
| 44 | $oStyle->__set("outlinecolor", $oOutlineColor); |
| 45 | $oClass->insertStyle($oStyle); |
| 46 | $oPolyLayer->SetProjection("init=EPSG:3978"); |
| 47 | // set opacity (actually creates a new COMPOSITE object) |
| 48 | $oPolyLayer->setOpacity(40); |
| 49 | |
| 50 | //****** |
| 51 | // create new WMS layer |
| 52 | //****** |
| 53 | $oWMSLayer = new layerObj($oMap); |
| 54 | $oWMSLayer->__set("name", "cities"); |
| 55 | $oWMSLayer->__set("type", MS_LAYER_RASTER); |
| 56 | $oWMSLayer->__set("status", MS_ON); |
| 57 | $oWMSLayer->setConnectionType(MS_WMS, NULL); |
| 58 | $oWMSLayer->__set("connection", "https://demo.gatewaygeomatics.com/cgi-bin/wms_gateway?"); |
| 59 | $oWMSLayer->setMetaData("wms_name", "popplace"); |
| 60 | $oWMSLayer->setMetaData("wms_srs", "EPSG:3978"); |
| 61 | $oWMSLayer->setMetaData("wms_server_version", "1.1.1"); |
| 62 | $oWMSLayer->setMetaData("wms_format", "image/png"); |
| 63 | $oWMSLayer->SetProjection("init=EPSG:3978"); |
| 64 | |
| 65 | //****** |
| 66 | // create new point layerObj |
| 67 | //****** |
| 68 | $oPointLayer = new layerObj($oMap); |
| 69 | $oPointLayer->__set("name", "gg-location"); |
| 70 | $oPointLayer->__set("type", MS_LAYER_POINT); |
| 71 | $oPointLayer->__set("status", MS_ON); |
| 72 | $oPointLayer->__set("units", "DD"); |
| 73 | $oPoint = new pointObj(-64.2576966,44.3596731); |
| 74 | // create new lineObj (lineObj is composed of one or more pointObj instances) |
| 75 | $oline = new lineObj(); |
| 76 | $oline->add($oPoint); |
| 77 | // create shapeObj for point |
| 78 | $oShape = new shapeObj(); |
| 79 | $oShape->add($oline); |
| 80 | $oPointLayer->addFeature($oShape); |
| 81 | // create new class for poly layer |
| 82 | $oClass = new classObj($oPointLayer); |
| 83 | $oClass->__set("name", "GatewayGeo"); |
| 84 | // create new style |
| 85 | $oStyle = new styleObj(); |
| 86 | // set symbol |
| 87 | $oStyle->setSymbolByName($oMap, "star"); |
| 88 | $oStyle->__set("size", 10); |
| 89 | // create new colors |
| 90 | $oColor = new colorObj(255,255,0); |
| 91 | $oStyle->__set("color", $oColor); |
| 92 | $oClass->insertStyle($oStyle); |
| 93 | $oPointLayer->SetProjection("init=EPSG:4326"); |
| 94 | |
| 95 | // draw map |
| 96 | $oImage = $oMap->draw(); |
| 97 | |
| 98 | // save image file |
| 99 | //$file = $oImage->save("C:/ms4w/apps/phpmapscriptng-swig/ttt.png",$oMap); |
| 100 | |
| 101 | // save mapfile to new file |
| 102 | //$oMap->save("C:/ms4w/apps/phpmapscriptng-swig/output.map"); |
| 103 | |
| 104 | // set header |
| 105 | header("Content-type: image/png"); |
| 106 | |
| 107 | //send image to stdout, without saving file locally |
| 108 | echo $oImage ->getBytes(); |
| 109 | |
| 110 | ?> |
| 111 | }}} |