| | 61 | == Example#2 == |
| | 62 | |
| | 63 | The example below works under MapServer 5.2.1 (tested under Fedora 10). Contributed by Tim Wood. |
| | 64 | |
| | 65 | {{{ |
| | 66 | <?PHP |
| | 67 | |
| | 68 | /** |
| | 69 | * Code to create a map from a shapefile |
| | 70 | * and then (optionally) reproject the whole thing |
| | 71 | * |
| | 72 | * If you just want to look at the reprojection stuff, start |
| | 73 | * with the "reproject (if needed) and draw" section at the end. |
| | 74 | * The rest |
| | 75 | * |
| | 76 | * Created by Tim Wood from code and input |
| | 77 | * by Tom Kralidis [Ontario], Pietro Gianninni and Stephen Jones. |
| | 78 | * |
| | 79 | * No copyright restrictions |
| | 80 | */ |
| | 81 | |
| | 82 | |
| | 83 | // --------------------------- |
| | 84 | // Configuration |
| | 85 | // aka set a bunch of variables and load the mapscript extension |
| | 86 | // |
| | 87 | |
| | 88 | // Where my key directories are located |
| | 89 | $doc_root = '/var/www/html'; |
| | 90 | $rel_path = "example1/round2"; |
| | 91 | |
| | 92 | $base_path = "$doc_root/$rel_path"; |
| | 93 | |
| | 94 | |
| | 95 | // An empty map file |
| | 96 | $base_map = "$base_path/data/empty.map"; // just the words MAP and END on dif. lines... really |
| | 97 | |
| | 98 | // The data I'm using |
| | 99 | $shape_path = "$base_path/data/zcta/zt08_d00_shp/"; // a goofy path I'm too lazy to fix |
| | 100 | $shape_file = "gfsa000b06a_e"; // works with or w/o the extension |
| | 101 | $shape_file_projection = "+proj=longlat +datum=NAD83"; |
| | 102 | |
| | 103 | // The Extents (area) I want in latitude, longitude |
| | 104 | list( $ex0, $ex1, $ex2, $ex3 ) = array(-140, 40, -50, 55 ); |
| | 105 | |
| | 106 | // Describe the rest of my output |
| | 107 | $output_projection = ''; |
| | 108 | $output_projection = "+proj=tcc +lon_0=90w +ellps=GRS80"; |
| | 109 | $output_file_rel = "output/index2.png"; |
| | 110 | $output_file_path = "$base_path/$output_file_rel"; |
| | 111 | list($output_width, $output_height ) = array( 600, 500 ); |
| | 112 | |
| | 113 | // Load MapScript extension |
| | 114 | if (!extension_loaded("MapScript")) dl('php_mapscript.'.PHP_SHLIB_SUFFIX); |
| | 115 | |
| | 116 | |
| | 117 | |
| | 118 | |
| | 119 | // --------------------------- |
| | 120 | // Function(s) |
| | 121 | // |
| | 122 | |
| | 123 | /** |
| | 124 | * Reproject a $map from $shape_file_projection to $output_projection |
| | 125 | */ |
| | 126 | function reproject_map( &$map, $shape_file_projection, $output_projection ) { |
| | 127 | $origProjObj = ms_newProjectionObj( $shape_file_projection ); |
| | 128 | $newProjObj = ms_newProjectionObj( $output_projection ); |
| | 129 | |
| | 130 | $oRect = $map->extent; |
| | 131 | $oRect->project( $origProjObj, $newProjObj ); |
| | 132 | $map->setExtent( $oRect->minx, $oRect->miny, $oRect->maxx, $oRect->maxy ); |
| | 133 | $map->setProjection( $output_projection ); |
| | 134 | } |
| | 135 | |
| | 136 | |
| | 137 | |
| | 138 | // --------------------------- |
| | 139 | // The Main Code |
| | 140 | // |
| | 141 | |
| | 142 | // add map |
| | 143 | $map = ms_newMapObj( $base_map ); |
| | 144 | $map->set( 'name', 'my_map' ); // If we're using a blank map file, give it a name |
| | 145 | |
| | 146 | // Set the extent |
| | 147 | $map->setExtent( $ex0, $ex1, $ex2, $ex3 ); |
| | 148 | |
| | 149 | // Set the shapepath |
| | 150 | $map->set( 'shapepath', $shape_path ); |
| | 151 | // Set the output format and size |
| | 152 | $map->selectOutputFormat( 'png' ); |
| | 153 | $map->setSize( $output_width, $output_height ); |
| | 154 | |
| | 155 | // add new layer to map |
| | 156 | $layer = ms_newLayerObj($map); |
| | 157 | $layer->set("name", "foo"); |
| | 158 | $layer->set("status", MS_ON); |
| | 159 | $layer->set("data", $shape_file ); |
| | 160 | $layer->set("type", MS_SHAPE_POLYGON ); |
| | 161 | $layer->setProjection( $shape_file_projection ); |
| | 162 | $layer->set("template", "ttt"); // hide errors that appear iff $output_projection is set? |
| | 163 | $layer->set("dump", "true"); |
| | 164 | |
| | 165 | // add new class to new layer |
| | 166 | $class = ms_newClassObj($layer); |
| | 167 | $class->set("name", "foo"); |
| | 168 | |
| | 169 | // add new style to new class |
| | 170 | $style = ms_newStyleObj($class); |
| | 171 | $style->color->setRGB(255, 0, 0); |
| | 172 | $style->outlinecolor->setRGB( 128,128,128 ); |
| | 173 | |
| | 174 | // reproject (if needed) and draw |
| | 175 | if( $output_projection != '' ) { |
| | 176 | reproject_map( $map, $shape_file_projection, $output_projection ); |
| | 177 | $image = $map->drawQuery(); |
| | 178 | } else { |
| | 179 | // draw |
| | 180 | $image = $map->draw(); |
| | 181 | } |
| | 182 | |
| | 183 | |
| | 184 | |
| | 185 | // --------------------------- |
| | 186 | // Output the image |
| | 187 | // |
| | 188 | |
| | 189 | $image->saveImage( $output_file_path ); |
| | 190 | |
| | 191 | ?> |
| | 192 | <html> |
| | 193 | <body> |
| | 194 | <img src="<?PHP print $output_file_rel; ?>"> |
| | 195 | </body> |
| | 196 | </html> |
| | 197 | }}} |
| | 198 | |