Spatial data viewing from postgres database in the visual studio based FORM using C#

Topics: SharpMap Project
May 8 at 2:42 PM
I have made a user interactive application using c# in visual studio and connected it to postgres database. It has some shape files. I need to automate the process of map generation directly from the application.

Currently I am using Sharpmap and its extensions for the purpose. But i wrote this code and there is no result. It doesnt show error but also no result at all.
using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Npgsql;
using SharpMap;
using SharpMap.Extensions;
using SharpMap.Data.Providers;
using GeoAPI;
using SharpMap.Layers;

 namespace ver2
 {
   public partial class Form1 : Form
   {
     Map mymap;

      public Form1()
       {
         InitializeComponent();

       }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void button1_Click(object sender, EventArgs e)
    {
        mymap = new Map(pictureBox1.Size);
        VectorLayer v = new VectorLayer("road", new PostGIS("Server =    localhost; Port = 5432; User Id = postgres; Password = hell08; Database = db1", "road", "geom", "gid"));
        mymap.Layers.Add(v);
        mymap.Layers.Add(v);
        mymap.ZoomToExtents();
        pictureBox1.Image = mymap.GetMap();

       }

     }
 }
May 8 at 8:58 PM
It seems you should add mymap to map property of a mapcontrol
May 11 at 7:51 AM
Thankyou for the reply but Please can you be more specific ? I urgently need this to be answered, as I am doing this as a part of an academic project which needs to be submitted by month's end.

Thanks !
Coordinator
May 11 at 9:25 AM
  1. You are adding the same layer twice, you should be seeing a DuplicateLayerException.
  2. What am2 wants to say is that there is a MapBox control that takes care of rendering the map, adjusting the viewport, zooming, panning, etc, that you should be using instead of adding a "GetMap"ed image pasted to a picture box.
You can test that your datasource is correct by splitting the creation of the provider out of the constructor to VectorLayer.
var p = new PostGIS("Server =    localhost; Port = 5432; User Id = postgres; Password = hell08; Database = db1", "road", "geom", "gid");
System.Diagnostics.Debug.Assert(p.GetFeatureCount() > 0);
var v = new VectorLayer("road", p);
May 12 at 7:04 PM
        var p = new PostGIS("Server = 127.0.0.1; Port = 5432; User Id = postgres; Password = hell08; Database = db1", "road", "geom", "gid");
        System.Diagnostics.Debug.Assert(p.GetFeatureCount() > 0);
        VectorLayer v = new VectorLayer("road", p);
        mapBox1.Map.Layers.Add(v);
        mapBox1.Map.ZoomToExtents();

There is no problem with the datasource and feature count is > o
But still nothing is displayed in the mapbox
May 12 at 7:05 PM
    var p = new PostGIS("Server = 127.0.0.1; Port = 5432; User Id = postgres; Password = hell08; Database = db1", "road", "geom", "gid");
    System.Diagnostics.Debug.Assert(p.GetFeatureCount() > 0);
    VectorLayer v = new VectorLayer("road", p);
    mapBox1.Map.Layers.Add(v);
    mapBox1.Map.ZoomToExtents();
There is no problem with the datasource and feature count is > o
But still nothing is displayed in the mapbox
Coordinator
May 13 at 7:35 AM
What kind of data is it?
You also may want to check that the extent is not empty:
System.Diagnostics.Debug.Assert(p.GetExtents().Width > 0);
System.Diagnostics.Debug.Assert(p.GetExtents().Height > 0);
Additionally you can force a refresh of the control. This may be needed, at least the sample code I have at hand does it this way.
mapBox1.Map.Layers.Add(v);
mapBox1.Map.ZoomToExtents();
mapBox1.Refresh();
May 13 at 4:05 PM
Both the above statements result in the following exception :

An unhandled exception of type 'Npgsql.NpgsqlException' occured : "42883: no binary output function available for type box2d"


DATA :

I have some shape files , which I converted to postgres POSTGIS tables using an automated plugin available "POSTGIS Shapefile Import/Export Manager"

I can not directly access and work with shapefiles as the non-spatial data is dyanamic and will change to and fro based on user queries. Else when i use shapefile as the datasource instead of POSTGIS my code works absolutely fine.
Coordinator
May 13 at 10:04 PM
So there is some issue with the server.
You are sure that you have postgis installed on the server and enabled the extension on the database you are using? Which version is it (SELECT postgis_full_version();)?
May 15 at 2:33 PM
"POSTGIS="2.2.2 r14797" GEOS="3.5.0-CAPI-1.9.0 r4090" SFCGAL="1.3.0" PROJ="Rel. 4.9.1, 04 March 2015" GDAL="GDAL 2.0.2, released 2016/01/26" LIBXML="2.7.8" LIBJSON="0.12" TOPOLOGY RASTER"
Coordinator
May 17 at 10:13 AM
I have no postgis 2.2 installation at hand, so it might be an issue with that.
If possible, you could try to install postgis legacy functions on your server: contrib/share/postgis-2.2/legacy.sql
May 18 at 2:40 PM
Thanks for your help FObermaier. But I am fed up now and will work with shapefiles for the time. Please tell me reliable sources to work with different aspects ( Eg. accessing a particular feature in the shapefile, re-writing the shapefile etc)

Thanks a lot !
May 26 at 8:12 PM
Hi FObermaier !!

Let me tell you the entire project I am working on

I have few shapefiles and related nonspatial data. They are buildings, trees, Parkings etc of a particular place. Now I have to let user interact with the non spatial info ( usual db queries - Taken care of ) and the results should be displayed in a form of map

FOR EG: the building with highest electricity consumption last year ! the query will show the value and building name but simultaneously the building should be highlighted ith a different color on the map.

For this I have been using visual studio 2015 community version with C#
i designed a few forms and connected them with db ( mysql -> postgres -> sql server )
but the map rendering always posed a problem . I even chanhed the db as said above to solve the problem but all in vain,

Now i thought to work to work directly with shapefiles but we can't access a particular feature in a shapefile also ... as non spatial info change frequently ..then re-writing shapefile would be tedious

Please help!!
Coordinator
May 27 at 6:31 AM
Feel free to contact me to send your code.
May 27 at 7:55 PM
Is there any way to convert the table from SQL server to shapefile using C# code ?? That would solve a huge problem for me. I have been through various sources and came across ogr2ogr but i don't know how to use it. Can you please suggest a way that i code into my c# code.
Coordinator
May 28 at 3:34 PM
SharpMap has no means of writing shapefiles.
You could succeed writing a FeatureDataTable to a shapefile using Ogr(Provider) of the SharpMap.Extensions package.