| 1 | = Add Layer, Query and Reproject = |
| 2 | |
| 3 | Here's a small example which: |
| 4 | |
| 5 | * opens a mapfile |
| 6 | * adds a new layer |
| 7 | * queries and new layer |
| 8 | * reprojects output map |
| 9 | * draws and saves to file |
| 10 | |
| 11 | |
| 12 | Example based on http://dl.maptools.org/dl/omsug/osgis2004/php_mapscript_mum2.ppt in response to http://n2.nabble.com/Problems-with-MapScript-and-setProjection-tt2291579.html#none |
| 13 | |
| 14 | {{{ |
| 15 | <?php |
| 16 | |
| 17 | // load mapscript |
| 18 | dl("php_mapscript.so"); |
| 19 | |
| 20 | // add map |
| 21 | $map = ms_newMapObj("config.map"); |
| 22 | |
| 23 | // add new layer to map |
| 24 | $layer = ms_newLayerObj($map); |
| 25 | $layer->set("name", "foo"); |
| 26 | $layer->set("status", MS_ON); |
| 27 | $layer->set("data", "path/to/file.shp"); |
| 28 | $layer->set("type", MS_LAYER_POINT); |
| 29 | $layer->setProjection("init=epsg:4326"); |
| 30 | $layer->set("template", "ttt"); |
| 31 | $layer->set("dump", "true"); |
| 32 | |
| 33 | // add new class to new layer |
| 34 | $class = ms_newClassObj($layer); |
| 35 | $class->set("name", "foo"); |
| 36 | |
| 37 | // add new style to new class |
| 38 | $style = ms_newStyleObj($class); |
| 39 | $style->set("symbol", 2); |
| 40 | $style->set("size", 8); |
| 41 | $style->color->setRGB(255, 0, 0); |
| 42 | |
| 43 | // create new rect to query against the new layer |
| 44 | $rect = ms_newRectObj(); |
| 45 | $rect->setExtent(-95, 40, -50, 60); |
| 46 | |
| 47 | // query new layer |
| 48 | $layer->queryByRect($rect); |
| 49 | |
| 50 | // set projection of output map |
| 51 | $map->setProjection("init=epsg:42304", MS_TRUE); |
| 52 | // draw |
| 53 | $image=$map->drawQuery(); |
| 54 | $image->saveImage("/tmp/out.png"); |
| 55 | |
| 56 | ?> |
| 57 | }}} |