[mapserver-users] How to draw line on Imagebutton
venkat
ven.tammineni at gmail.com
Thu Sep 16 04:34:36 PDT 2010
Dear All,
I want to draw line object on map (imagebutton).I do not know how to
capture and fist click(xy) and where to store again i need to get second
click( second point xy)..and finally i need to draw line on the map in
server side(C# or whatever) .I can able to draw point.But i am not able to
draw line object and below is my code... Please anyone can help me.
public static void AddFeature(Double x, Double y, String activeLayer,
mapObj mainMapObject)
{
try
{
//check if the active layer is a point layer and if the
point layer is from PostGIS
LayerObject.Layer =
mainMapObject.getLayerByName(activeLayer);
if (LayerObject.Layer.type != MS_LAYER_TYPE.MS_LAYER_LINE)
{
return;
}
//convert the image point in map point Here I am getting
issue
pointObj mapPointObj = Pixel2Geo(new pointObj(x, y, 0, 0),
mainMapObject);
pointObj secondPoint = Pixel2Geo(new
pointObj(0,0,0,0),mainMapObject);
//add the point to a PostGIS table
if (LayerObject.Layer.connectiontype ==
MS_CONNECTION_TYPE.MS_POSTGIS)
{
//set CurrentCulture according to PostgreSQL server
CultureInfo newCultureInfo = new CultureInfo("en-US");
newCultureInfo.NumberFormat.NaNSymbol = "";
Thread.CurrentThread.CurrentCulture = newCultureInfo;
//connect with PostgreSQL
String sqlInsert = "INSERT INTO " + activeLayer +
"(name,the_geom) VALUES('ven',GeomFromText('LINESTRING(" +
mapPointObj.x.ToString() + " " + mapPointObj.y.ToString() + "," +
secondPoint.x.ToString()+ "" + secondPoint.y.ToString() + ")',24047))";
//reads connection string for PostgreSQL
Npgsql.NpgsqlConnection npgSqlConnection = new
Npgsql.NpgsqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
npgSqlConnection.Open();
Npgsql.NpgsqlCommand cmdCreator =
npgSqlConnection.CreateCommand();
cmdCreator.CommandText = sqlInsert;
cmdCreator.ExecuteNonQuery();
npgSqlConnection.Close();
}
}
catch (Exception exp)
{ };
}
public static pointObj Pixel2Geo(OSGeo.MapServer.pointObj pt, mapObj
map)
{
// Converstion of pixel coordinates to Geographic coordinates
int imgXMin, imgXMax, imgYMin, imgYMax;
Double geoXMin, geoXMax, geoYMin, geoYMax;
//init corner coords
imgXMin = 0;
imgYMin = 0;
imgXMax = map.width;
imgYMax = map.height;
geoXMin = map.extent.minx;
geoXMax = map.extent.maxx;
geoYMin = map.extent.miny;
geoYMax = map.extent.maxy;
int imgWidth, imgHeight;
System.Double geoWidth, geoHeight;
//calc the width
imgWidth = imgXMax - imgXMin;
imgHeight = imgYMax - imgYMin;
if (geoXMin < geoXMax)
{
geoWidth = geoXMax - geoXMin;
}
else
{
geoWidth = geoXMin - geoXMax;
}
if (geoYMin < geoYMax)
{
geoHeight = geoYMax - geoYMin;
}
else
{
geoHeight = geoYMin - geoYMax;
}
//calc the percent along each axis
Double xPercent, yPercent;
xPercent = pt.x / imgWidth;
yPercent = pt.y / imgHeight;
Double newX, newY;
newX = (xPercent * geoWidth) + geoXMin;
newY = geoYMax - (yPercent * geoHeight);
return new OSGeo.MapServer.pointObj(newX, newY, 0.0, 0.0);
}
}
Thanks and Regards,
Ven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/mapserver-users/attachments/20100916/8ad6baa1/attachment.htm>
More information about the MapServer-users
mailing list