= Add Layer, Query and Reproject = == Example 1 == Here's a small example which: * opens a mapfile * adds a new layer * queries new layer * reprojects output map * draws and saves to file 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 Contributed by Tom Kralidis. {{{ set("name", "foo"); $layer->set("status", MS_ON); $layer->set("data", "path/to/file.shp"); $layer->set("type", MS_LAYER_POINT); $layer->setProjection("init=epsg:4326"); $layer->set("template", "ttt"); $layer->set("dump", "true"); // add new class to new layer $class = ms_newClassObj($layer); $class->set("name", "foo"); // add new style to new class $style = ms_newStyleObj($class); $style->set("symbol", 2); $style->set("size", 8); $style->color->setRGB(255, 0, 0); // create new rect to query against the new layer $rect = ms_newRectObj(); $rect->setExtent(-95, 40, -50, 60); // query new layer $layer->queryByRect($rect); // set projection of output map $map->setProjection("init=epsg:42304", MS_TRUE); // draw $image=$map->drawQuery(); $image->saveImage("/tmp/out.png"); ?> }}} == Example 2 == The example below works under !MapServer 5.2.1 (tested under Fedora 10). Contributed by Tim Wood. {{{ extent; $oRect->project( $origProjObj, $newProjObj ); $map->setExtent( $oRect->minx, $oRect->miny, $oRect->maxx, $oRect->maxy ); $map->setProjection( $output_projection ); } // --------------------------- // The Main Code // // add map $map = ms_newMapObj( $base_map ); $map->set( 'name', 'my_map' ); // If we're using a blank map file, give it a name // Set the extent $map->setExtent( $ex0, $ex1, $ex2, $ex3 ); // Set the shapepath $map->set( 'shapepath', $shape_path ); // Set the output format and size $map->selectOutputFormat( 'png' ); $map->setSize( $output_width, $output_height ); // add new layer to map $layer = ms_newLayerObj($map); $layer->set("name", "foo"); $layer->set("status", MS_ON); $layer->set("data", $shape_file ); $layer->set("type", MS_SHAPE_POLYGON ); $layer->setProjection( $shape_file_projection ); $layer->set("template", "ttt"); // hide errors that appear iff $output_projection is set? $layer->set("dump", "true"); // add new class to new layer $class = ms_newClassObj($layer); $class->set("name", "foo"); // add new style to new class $style = ms_newStyleObj($class); $style->color->setRGB(255, 0, 0); $style->outlinecolor->setRGB( 128,128,128 ); // reproject (if needed) and draw if( $output_projection != '' ) { reproject_map( $map, $shape_file_projection, $output_projection ); $image = $map->drawQuery(); } else { // draw $image = $map->draw(); } // --------------------------- // Output the image // $image->saveImage( $output_file_path ); ?> }}} ---- back to [wiki:PHPMapScript]