From: Regina Obe Date: Mon, 26 Dec 2011 22:10:45 +0000 (+0000) Subject: c# example using ST_AsPNG X-Git-Tag: 2.0.0alpha1~287 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=81214cb36eb2350e3d7f73a923a644d1f6720380;p=postgis c# example using ST_AsPNG git-svn-id: http://svn.osgeo.org/postgis/trunk@8588 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/doc/using_raster_dataman.xml b/doc/using_raster_dataman.xml index 33b34fa64..78ab29d27 100644 --- a/doc/using_raster_dataman.xml +++ b/doc/using_raster_dataman.xml @@ -511,7 +511,7 @@ Available GDAL raster formats: $conn_str ='dbname=mydb host=localhost port=5432 user=myuser password=mypwd'; $dbconn = pg_connect($conn_str); header('Content-Type: image/png'); -/**If a particular projection was requested use it otherwise use mass state plane feet **/ +/**If a particular projection was requested use it otherwise use mass state plane meters **/ if (!empty( $_REQUEST['srid'] ) && is_numeric( $_REQUEST['srid']) ){ $input_srid = intval($_REQUEST['srid']); } @@ -531,5 +531,81 @@ if ($row === false) return; echo pg_unescape_bytea($row[0]); ?>]]> + + ASP.NET C# Example Outputting using ST_AsPNG in concert with other raster functions + In this section, we'll demonstrate how to use Npgsql PostgreSQL .NET driver and the family of functions to + output band 1,2,3 of a raster to a PHP request stream that can then be embedded in an img src html tag. + + The sample query demonstrates how to combine a whole bunch of raster functions together to grab all tiles that intersect + a particular wgs 84 bounding box and then unions with the intersecting tiles together returning all bands, transforms to user specified projection using , + and then outputs the results as a png using . + This is same example as except implemented in C#. + -- web.config connection string section -- + + +]]> + // Code for TestRaster.ashx + +using System; +using System.Data; +using System.Web; +using Npgsql; + +public class TestRaster : IHttpHandler +{ + public void ProcessRequest(HttpContext context) + { + + context.Response.ContentType = "image/png"; + context.Response.BinaryWrite(GetResults(context)); + + } + + public bool IsReusable { + get { return false; } + } + + public byte[] GetResults(HttpContext context) + { + byte[] result = null; + NpgsqlCommand command; + string sql = null; + int input_srid = 26986; + try { + using (NpgsqlConnection conn = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DSN"].ConnectionString)) { + conn.Open(); + + if (context.Request["srid"] != null) + { + input_srid = Convert.ToInt32(context.Request["srid"]); + } + sql = @"SELECT ST_AsPNG( + ST_Transform( + ST_AddBand( + ST_Union(rast,1), ARRAY[ST_Union(rast,2),ST_Union(rast,3)]) + ,:input_srid) ) As new_rast + FROM aerials.boston + WHERE + ST_Intersects(rast, + ST_Transform(ST_MakeEnvelope(-71.1217, 42.227, -71.1210, 42.218,4326),26986) )"; + command = new NpgsqlCommand(sql, conn); + command.Parameters.Add(new NpgsqlParameter("input_srid", input_srid)); + + + result = (byte[]) command.ExecuteScalar(); + conn.Close(); + } + + } + catch (Exception ex) + { + result = null; + context.Response.Write(ex.Message.Trim()); + } + return result; + } +}]]> +