Dynamic info upgrade

Oct 8, 2014 at 3:41 PM
Hi guys,
I've just started using SharpMap ... and I like it :-) but I still don't know how to use it in the right way.
Hope someone can help me to solve this issue:
I'm trying to represent on a map (I'm using a couple of .shp for now) some vehicles.
Each vehicle must have a label with some specific info.
Now, I'm able to load layers:
NetTopologySuite.Geometries.GeometryFactory _geometryFactor = new NetTopologySuite.Geometries.GeometryFactory();

SharpMap.Layers.VectorLayer _vehiclesLayer = new SharpMap.Layers.VectorLayer("VEHICLES");

List<GeoAPI.Geometries.IGeometry> _vehiclesLayerFeatureSet = new List<GeoAPI.Geometries.IGeometry>();

SharpMap.Data.Providers.GeometryProvider _vehiclesLayerSourceProvider = new SharpMap.Data.Providers.GeometryProvider(_vehiclesLayerFeatureSet);

_vehiclesLayer.DataSource = _vehiclesLayerSourceProvider;
_vehiclesLayer.Style.Symbol = Bitmap.FromFile(@"C:\Icons\testIcon.ico");
_vehiclesLayer.Style.SymbolScale = 0.5F;
_vehiclesLayer.Style.Fill = Brushes.Transparent;

_map.Map.VariableLayers.Add(_vehiclesLayer);

FeatureDataTable _vehicleLayerFeatureDataTable = new FeatureDataTable();
_vehicleLayerFeatureDataTable.Columns.Add("Label", typeof(string));
_vehicleLayerFeatureDataTable.Columns.Add("ID", typeof(int));

SharpMap.Layers.LabelLayer _vehiclesLabelLayer = new SharpMap.Layers.LabelLayer("VEHICLES LABELS");

_vehiclesLabelLayer.DataSource = new SharpMap.Data.Providers.GeometryFeatureProvider(_vehicleLayerFeatureDataTable);
_vehiclesLabelLayer.Enabled = true;
_vehiclesLabelLayer.LabelColumn = "Label";

_vehiclesLabelLayer.Style = new SharpMap.Styles.LabelStyle();
_vehiclesLabelLayer.Style.ForeColor = Color.DarkBlue;
_vehiclesLabelLayer.Style.Font = new Font(FontFamily.GenericSansSerif, 12);
_vehiclesLabelLayer.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Top;
_vehiclesLabelLayer.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center;
_vehiclesLabelLayer.Style.Offset = new PointF(5, 5);
_vehiclesLabelLayer.Style.Halo = new Pen(Color.Snow, 1);
_vehiclesLabelLayer.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

_map.Map.VariableLayers.Add(_vehiclesLabelLayer);
show "vehicle" on the map:
IPoint point = _geometryFactor.CreatePoint(vehicleMapPoint);
point.SRID = _vehiclesLayerFeatureSet.Count + 1;

_vehiclesLayerFeatureSet.Add(point);

_vehiclesLayerSourceProvider.Geometries = _vehiclesLayerFeatureSet;
and define its label:
FeatureDataRow fdr = _vehicleLayerFeatureDataTable.NewRow();
fdr.Geometry = point;
fdr["Label"] = TEST LABEL";
fdr["ID"] = point.SRID;
_vehicleLayerFeatureDataTable.AddRow(fdr);
but, obviously, vehicles positions change so I have to update their position on the map:
IPoint point = (IPoint)_vehiclesLayerFeatureSet.Where(p => p.SRID == 1).FirstOrDefault();
if (point != null) {
    if (vehicleMapPoint != null) {

        point.X = vehicleMapPoint.X;
        point.Y = vehicleMapPoint.Y;

        _map.Refresh();

    }
}
and also their labels ..... and this is the issue: I can see vehicle position upgrading but its label remains in the old position.

Could someone explain me how to do it ?

Thanks in advance
Coordinator
Oct 11, 2014 at 8:51 PM
First of all, you seem to be abusing the SRID. It is an acronym for Spatial Reference Identifier and has nothing to do with an object id or so.
If you want to use variable layers, I strongly urge you not to rely on FeatureDataTable and GeometryFeatureProvider. I don't know where your data is coming from, but even if it is not Shapefile, PostGis, SqlServer, ..., implementing IProvider is not all that difficult.
If you add layers based on such an provider to the VariableLayersCollection, they will be queried every interval and the image will be updated.
Oct 13, 2014 at 8:40 AM
Hi FObermaier, first of all thanks for your answer.

As you can easly understand I've just started with this excellent tool and I don't know yet its features and how it can be used in the right way.

The code you see is the result of the informations that I've collected simply surfing on the site in a couple of day so I know that I'm so far from a correct implementation.

Now I think I've understood the SRID meaning and I suppose that I can use a "UserData" property to store "personal" user information.

With regard to VariableLayers, I admit that I wasn't sure it could be the right solution for me, I just was trying to make some test in order to understand.

I would profit of your competence and patience in order to understand SharpMap usage, so please let me better explain my needs.

I have some shape files containing information of my region; I should use them as background in order to represent a urban map on which show vehicles position.

These position come from an external service that periodically transmit vehicles coordinates and I receive them through a direct socket connection so I don't read them from a DB.

For this reason I supposed that I could use a vector layer for vehicles representation
NetTopologySuite.Geometries.GeometryFactory _geometryFactor = new NetTopologySuite.Geometries.GeometryFactory();

SharpMap.Layers.VectorLayer _vehiclesLayer = new SharpMap.Layers.VectorLayer("VEHICLES");

List<GeoAPI.Geometries.IGeometry> _vehiclesLayerFeatureSet = new List<GeoAPI.Geometries.IGeometry>();

SharpMap.Data.Providers.GeometryProvider _vehiclesLayerSourceProvider = new SharpMap.Data.Providers.GeometryProvider(_vehiclesLayerFeatureSet);

_vehiclesLayer.DataSource = _vehiclesLayerSourceProvider;
_vehiclesLayer.Style.Symbol = Bitmap.FromFile(@"C:\Icons\testIcon.ico");
_vehiclesLayer.Style.SymbolScale = 0.5F;
_vehiclesLayer.Style.Fill = Brushes.Transparent;

_map.Map.Layers.Add(_vehiclesLayer);
and add vehicles position (or update them) while receiving from my source
IPoint point = _geometryFactor.CreatePoint(vehicleMapPoint);

_vehiclesLayerFeatureSet.Add(point);

_vehiclesLayerSourceProvider.Geometries = _vehiclesLayerFeatureSet;
If this is correct, I should also use labels and this is my difficulty, I haven't understood how to do it.

Would you please help me and give me a such kind of example ?

Thanks in advance and excuse for my english.
Coordinator
Oct 13, 2014 at 11:22 AM
These position come from an external service that periodically transmit vehicles coordinates and I receive them through a direct socket connection so I don't read them from a DB.
Nonetheless you should create an IProvider implementation for your vehicle data. There is a commented example in the WinFormSamples project that queries bus/towntrain information from a web service periodically (search for Vilnius), that should get you started. The service is no longer active, that's why it is disabled.

Using VectorLayer is fine, you can reuse the provider used for VectorLayer with LabelLayer and you should be seeing the labels. How many vehicles are there?
Oct 13, 2014 at 1:06 PM
Hi,
where can I find the WinFormExamples you're talking about ? Is it avaible somewhere on the site ?

For what concerns the number of vehicles it depends, I mean that this fleet is composed by about 800 vehicles and I'm dealing wiht a Public Transport Company service monitoring so they aren't all active at the same time.

Thanks
Coordinator
Oct 13, 2014 at 2:09 PM
romerm wrote:
where can I find the WinFormExamples you're talking about ? Is it avaible somewhere on the site ?
You can download a lot of sample code if you go to the Source tab and browse for Branches/1.0. The file of particular interest is
https://sharpmap.codeplex.com/SourceControl/latest#Branches/1.0/Examples/WinFormSamples/Samples/TileLayerSample.cs
Search for "VilniusTransportData"
For what concerns the number of vehicles it depends, I mean that this fleet is composed by about 800 vehicles and I'm dealing wiht a Public Transport Company service monitoring so they aren't all active at the same time.
Just curiosity.
Oct 14, 2014 at 2:36 PM
Hi FObermaier,

I've checked the link you've sent me and I've found the solution to my problem.

Very useful, thanks so much.