Question on Point Images....

Topics: General Topics
Oct 11, 2006 at 9:31 PM
Hi Everyone,
I've been using SharpMap now for about a week and something is really confusing me. I am trying to assign an icon to a point (similar to those in Google Maps or VE). This project is for mapping schools in Alabama for crime rates etc. Anyhow, I have the map the way I want it, but can not get the 1400+ points to show either as dots or images. Does anyone have any code sample that might point me in the right direction? Any help would be much appreciated. Thans in advance...


Sincerely,
Dapanther
Coordinator
Oct 12, 2006 at 2:09 AM
I've gotten images to work with point data by assigning a layer style and setting the Image property to the desired image.

Let me know if you still have problems after looking at the layer's style, and I'll dig around for that sample.
Oct 12, 2006 at 6:22 AM
Hi

It's actualy quite straight forward - try something like this:

layadr.Style.SymbolScale = 1;
layadr.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\img\info.gif"));
myMap.Layers.Add(layadr);
Oct 12, 2006 at 5:01 PM
Hi again,
I posted the sub routine below that I am trying to insert the 1400+ schools. Am I missing something? Any help would be much appreciated. Thanks..

dapanther....

BEGIN CODE
Private Sub AddSchools(ByRef map As SharpMap.Map)
Dim schDS As New schools
Dim row As DataRow
For Each row In schDS.Tables(0).Rows
' add points
Dim VLayer As SharpMap.Layers.VectorLayer = New SharpMap.Layers.VectorLayer("GeometryLayer")
Dim p1 As System.Drawing.PointF = map.WorldToImage(New SharpMap.Geometries.Point(32.463014, -86.476367))
VLayer.DataSource = New SharpMap.Data.Providers.GeometryProvider(New SharpMap.Geometries.Point(p1.X, p1.Y))
VLayer.Style.Symbol.SetPixel(p1.X, p1.Y, Drawing.Color.Red)
VLayer.Style.Symbol.SetResolution(20, 20)
VLayer.Style.Fill = New System.Drawing.SolidBrush(Drawing.Color.Red)
VLayer.Style.Outline = System.Drawing.Pens.Red
VLayer.Style.EnableOutline = True
VLayer.Style.SymbolScale = 1
VLayer.Style.Symbol = New Drawing.Bitmap(HttpContext.Current.Server.MapPath("~\largeRed.gif"))
VLayer.SRID = 4326
map.Layers.Add(VLayer)
Next
End Sub

END CODE
Coordinator
Oct 12, 2006 at 6:55 PM
Not sure if this is just a sample abberation, but the For Each loop won't be executed in the sample... you don't have any rows in schDS since you have newly declared it in the body of the method.

Is it that you are seeing nothing, or just not the image you specified?
Dec 13, 2006 at 2:44 PM
Hi again,
I know it has been a while since I posted anything on this. I had a few projects that had some presidence over the GIS solution. Anyhow I corrected some code and have our maps working with the exception of the schools showing. Below is a snip of the code I am using now to display the schools points:

' add in the schools
Try
Dim ds As New DataSet
Dim dt As New DataTable
Dim dr As DataRow

Dim conn As New SqlConnection
conn.ConnectionString = "Server=JREYNOLDS\SQLEXPRESS;Database=GeoRSS;Trusted_Connection=True;"
Dim cmd As New SqlCommand("GetSchoolAbsence", conn)
cmd.CommandType = Data.CommandType.StoredProcedure
conn.Open()
Dim rdr = cmd.ExecuteReader

dt.Load(rdr)
ds.Tables.Add(dt)

rdr.close()
conn.Close()

Dim schLayer As New SharpMap.Layers.VectorLayer("SchoolLayer")
Dim GeomColl As New SharpMap.Geometries.GeometryCollection()

For Each dr In ds.Tables(0).Rows
If Not (dr(7).Equals("") And dr(8).Equals("")) Then
' add in the point to the collection
GeomColl.Collection.Add(New SharpMap.Geometries.Point(Convert.ToDouble(dr(7)), Convert.ToDouble(dr(8))))
End If
Next
schLayer.DataSource = New SharpMap.Data.Providers.GeometryProvider(GeomColl)
schLayer.Style.Symbol = New Drawing.Bitmap(HttpContext.Current.Server.MapPath("~\App_data\icon.png"))
schLayer.Style.SymbolScale = 0.5F
myMap.Layers.Add(GeomColl)
Catch ex As Exception

End Try


Any help with this would be much appreciated. Thanks in advance.

dapanther...
Dec 19, 2006 at 2:20 PM
Hi again all,
Ok, in a way I think I had a brain fart LOL I found the errors in the previous code, now I am error free but the points are not showing. Below is the corrected snip of code. Any light someone can shine on this would be greatly appreciated. Thanks.

' add in the schools
Try
Dim ds As New DataSet
Dim dt As New DataTable
Dim dr As DataRow

Dim conn As New SqlConnection
conn.ConnectionString = "Server=JREYNOLDS\SQLEXPRESS;Database=GeoRSS;Trusted_Connection=True;"
Dim cmd As New SqlCommand("GetLatLong", conn)
cmd.CommandType = Data.CommandType.StoredProcedure
conn.Open()
Dim rdr = cmd.ExecuteReader

dt.Load(rdr)
ds.Tables.Add(dt)

rdr.close()
conn.Close()

Dim count As Integer = 0

For Each dr In ds.Tables(0).Rows
If Not (dr(1).Equals("") And dr(2).Equals("")) Then
Dim schLayer As New SharpMap.Layers.VectorLayer("SchoolLayer" & count.ToString())
Dim GeomColl As New SharpMap.Geometries.GeometryCollection()
' add in the point to the collection
GeomColl.Collection.Add(New SharpMap.Geometries.Point(Convert.ToDouble(dr(1)), Convert.ToDouble(dr(2))))

schLayer.DataSource = New SharpMap.Data.Providers.GeometryProvider(GeomColl)
'schLayer.Style.Symbol = New Drawing.Bitmap(HttpContext.Current.Server.MapPath("~\App_data\icon.png"))
schLayer.Style.SymbolScale = 0.8F
myMap.Layers.Add(schLayer)
count = count + 1
schLayer = Nothing
GeomColl = Nothing
End If
Next
Catch ex As Exception
Dim conn As New SqlConnection
conn.ConnectionString = "Server=JREYNOLDS\SQLEXPRESS;Database=GeoRSS;Trusted_Connection=True;"
Dim cmd As New SqlCommand
cmd.Connection = conn
cmd.CommandText = "INSERT INTO tblAppError (AppName, AppErrorMsg, AppUser, AppErrorNumber, ErrorType, ErrorLvl, DateStamp) VALUES('GISApp', '" & ex.ToString().Replace("'", "") & "', '', '', 'ERROR', 10, '" & Date.Now.ToString() & "')"""
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Try
Dec 19, 2006 at 6:10 PM
Ok,
I figured out how to dynamically create a shapefile using a DLL from ArcScripts on ESRI's website. I created a shapefile based on the coordinates pulled from the DB and then added in the school names and their associated ID's.

When I look at the shapefile using a viewer all looks fine. The problem is when I go to import the shapefile into SharpMap and it still does not display the data. I have a shapefile for the state, the roads and cities (with labels) and can not get the schools to populate. Could it be the size of the dataset? It's coordinates and data for about 1600 schools. I've included the complete code below including the commented out sections. If anyone could enlighten me I would greatly appreciate it. Thanks in advance.



Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections



Public Class gen
Public Function GenMap(ByVal size As System.Drawing.Size) As SharpMap.Map

HttpContext.Current.Trace.Write("Initializing map...")

Dim myMap = New SharpMap.Map(size)

' All US State Lines
Dim us_states As New SharpMap.Layers.VectorLayer("Alabama")
' Specifing true will save the spatial index to a file which will make it load quicker the next time
usstates.DataSource = New SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath("~\Appdata\States\AL\AL_State.shp"), True)
' Set fill-style to salmon
us_states.Style.Fill = New Drawing.SolidBrush(Drawing.Color.NavajoWhite)
' Set the polygons to have a black outline
us_states.Style.Outline = System.Drawing.Pens.Black
us_states.Style.EnableOutline = True
us_states.SRID = 4326


' AL Roads
Dim al_roads As New SharpMap.Layers.VectorLayer("ALRoads")
alroads.DataSource = New SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath("~\Appdata\Roads\alrds.shp"), True)
al_roads.Style.Line = New Drawing.Pen(Drawing.Color.Black, 1)
al_roads.SRID = 4326

' Cities
'Dim al_cities As New SharpMap.Layers.VectorLayer("Cities")
'alcities.DataSource = New SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath("~\Appdata\Cities\AL\AL_Cities.shp"), True)
''Define a blue 1px wide pen
''alcities.Style.Symbol = New Drawing.Bitmap(HttpContext.Current.Server.MapPath("~\Appdata\icon.png"))
'al_cities.Style.SymbolScale = 0.8F
'al_cities.MaxVisible = 40
'al_cities.SRID = 4326

' City Labels
'Dim city_label As New SharpMap.Layers.LabelLayer("Cities")
'citylabel.DataSource = alcities.DataSource
'city_label.Enabled = True
'citylabel.LabelColumn = "CITYNAME"
'city_label.Style = New SharpMap.Styles.LabelStyle()
'city_label.Style.ForeColor = Drawing.Color.Black
'city_label.Style.Font = New Drawing.Font(Drawing.FontFamily.GenericSerif, 11)
'city_label.MaxVisible = 30
'city_label.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left
'city_label.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom
'city_label.Style.Offset = New Drawing.PointF(3, 3)
'city_label.Style.Halo = New Drawing.Pen(Drawing.Color.Yellow, 2)
'city_label.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
'city_label.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
'city_label.SRID = 4326
''citylabel.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection(citylabel)
'city_label.Style.CollisionDetection = True


' add in the schools
Dim al_Schools As New SharpMap.Layers.VectorLayer("Schools")
alSchools.DataSource = New SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath("~\Appdata\Schools\myShape.shp"), True)
'alcities.Style.Symbol = New Drawing.Bitmap(HttpContext.Current.Server.MapPath("~\Appdata\icon.png"))
al_Schools.Style.SymbolScale = 0.8F
al_Schools.MaxVisible = 50
al_Schools.SRID = 4326

' School Labels
Dim alSchoolslabel As New SharpMap.Layers.LabelLayer("SchoolLabels")
alSchoolslabel.DataSource = al_Schools.DataSource
alSchoolslabel.Enabled = True
alSchoolslabel.LabelColumn = "SchoolName"
alSchoolslabel.Style = New SharpMap.Styles.LabelStyle()
alSchoolslabel.Style.ForeColor = Drawing.Color.Black
alSchoolslabel.Style.Font = New Drawing.Font(Drawing.FontFamily.GenericSerif, 11)
alSchoolslabel.MaxVisible = 50
alSchoolslabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left
alSchoolslabel.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom
alSchoolslabel.Style.Offset = New Drawing.PointF(3, 3)
alSchoolslabel.Style.Halo = New Drawing.Pen(Drawing.Color.Yellow, 2)
alSchoolslabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
alSchoolslabel.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
alSchoolslabel.SRID = 4326
alSchoolslabel.Style.CollisionDetection = True

' add layers
myMap.Layers.Add(us_states)
myMap.Layers.Add(al_roads)
'myMap.Layers.Add(al_cities)
'myMap.Layers.Add(city_label)
myMap.Layers.Add(al_Schools)
myMap.Layers.Add(alSchoolslabel)

myMap.MaximumZoom = 360
myMap.BackColor = Drawing.Color.DodgerBlue
'myMap.ZoomtoExtents()

myMap.Zoom = 360

myMap.Center = New SharpMap.Geometries.Point(0, 0)
HttpContext.Current.Trace.Write("Map initialized")

' return the map object
Return myMap

End Function

End Class
Dec 19, 2006 at 7:23 PM
Have you considered these two lines:
myMap.Zoom = 360
myMap.Center = New SharpMap.Geometries.Point(0, 0)
If your data is in longitude/latitude, you are zooming to the entire extents of the earth and centering on greenwich/equator. I don't think your schools spreads out on that big an area.
If your data is NOT in long/lat but a different coordinate system, you are probably WAY of with this center and zoom.

try this instead:
myMap.ZoomToExtents()
or
or myMap.ZoomToBox(al_Schools.Extents();
Dec 20, 2006 at 1:42 PM
Thanks for the info. I actually found an error in the script I was using to generate the shapefile when I tried to open them again in ARC Explorer. I got it working now. There is another issue though that I have been trying to resolve.

With the points, I thought the MaxVisible or Style.MaxVisible would limit the number of points showing on the map. I am getting all 1600 of the schools trying to show at once which can be a little confusing. What I was going to try was using a series of check boxes to show more schools if the user wanted to see them but it appears as though the MaxVisible is not working correctly. The setting is in the code above for reference. Any ideas?

dapanther..
Dec 20, 2006 at 4:28 PM
You might want to look in the documentation for an explanation of these properties, instead of guessing. If you have copied the XML docs with the DLL, you would probably also get an explanation from intellisense.
Dec 22, 2006 at 1:29 PM
Odegaard,
Thanks for the reply. I misunderstood or had a brain fart when I read the docs. Sorry about that. I have the map working now. I guess I had a GIS newbie error LOL I actually had it working all along, I was referencing the Lat and Long's in reverse so the points were on the other side of the planet somewhere LMAO! Have a Happy Holiday...

Dapanther...