Adding Map Using PostGIS

Topics: Data Access, General Topics, SharpMap Project, SharpMap v0.9 / v1.x
Nov 21, 2012 at 3:55 PM
Edited Nov 21, 2012 at 4:15 PM

Hello

I am having some trouble adding a layer to a map with PostGIS and I am unsure why the problem is occurring. I have tried to implement code from other discussions on this forum.

 

 

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 SharpMap;
using Npgsql;
using System.Collections.ObjectModel;
using PostgreSql.Data.PgTypes;
using PostgreSql.Data.PostgreSqlClient;
using SharpMap.Converters.WellKnownBinary;
using SharpMap.Geometries;
using GeoAPI.Geometries;
using NetTopologySuite;

namespace SharpMapProject
{
    public partial class frmMap : Form
    {
        //Define The SharpMap Object
        SharpMap.Map IrishMap;

        //Zoom Factor
        const float Zoom_Factor = 0.3f;

        //Data & Data Path
        string data_Connection = "Server=127.0.0.1;Port=5432;UserId=postgres;Password=********;Database=postgis";
        string tablename = "county_region";
        string idColumn = "gid";
        string geomCol = "geom";

        

        public frmMap()
        {
            InitializeComponent();

            //Initialise the Map
            IrishMap = new SharpMap.Map(new System.Drawing.Size(600, 300));
            mapBox1.BackColor = Color.LightBlue;

            //Create Layers To Be Displayed
            SharpMap.Layers.VectorLayer counties = new SharpMap.Layers.VectorLayer("data_Connection");
            counties.DataSource = new SharpMap.Data.Providers.PostGIS(data_Connection, geomCol, idColumn);


            IrishMap.Layers.Add(counties);

            mapBox1.Map.Layers.Add(counties);

            mapBox1.Map.ZoomToExtents();

            mapBox1.Refresh();

            //Image img = IrishMap.GetMap();

            RefreshMap();


        }

        //Refresh Function
        private void RefreshMap()
        {
            //--> Use SharpMap to generate the map image
            mapBox1.Refresh();

        }
    }
}

I am getting the following error from the

 

counties.DataSource = new SharpMap.Data.Providers.PostGIS(data_Connection, geomCol, idColumn);

ERROR: 42883: function asbinary(geometry) does not exist

Developer
Nov 22, 2012 at 1:30 AM

You are may be using postgis 2.x. Please get new postgis provider from trunk

Nov 22, 2012 at 12:47 PM

Thanks for the reply I am currently using postgis-2.0.1. I'm pretty sure that is the latest version.

Nov 22, 2012 at 3:43 PM
Edited Nov 22, 2012 at 4:30 PM

I have tried building as x86 and x64 but still get the same error. If someone could help with this I would be very greatful. I cannot get any further until i can at least display layers on a map using PostGIS

Coordinator
Nov 23, 2012 at 6:30 AM
sim1986 wrote:

Thanks for the reply I am currently using postgis-2.0.1. I'm pretty sure that is the latest version.

That is not what trieuvy was referring to. With PostGis 2.0 some of the function that PostGis 1.x used to have were dropped, like e.g. "asbinary(geometry)" which has been replaced with st_asbinary(geometry).

Trieuvy has kindly updated SharpMap's postgis provider to handle this, so you need to update the that, not your postgis database :).

Hth FObermaier

Nov 23, 2012 at 12:58 PM

Apologies but how to i go about updating it I am new to this. I assumed I have all the components up to date as i have downloaded the latest trunk from the homepage.

Developer
Nov 23, 2012 at 1:31 PM

You can better download the latest source code. The PostGIS dataprovider there has this nice line of code which will fix your problem:

if (_postGisVersion >= 1.5) _prefixFunction = "ST_";

Nov 23, 2012 at 6:55 PM

I have downloaded the source code from the trunk just wondering now what do i do with it

Developer
Nov 23, 2012 at 8:27 PM

just take the Sharpmap.Extensions project and the Sharpmap project and add them to your solution. Set the references from your project to both Sharpmap projects in your solution and remove any existing references to the Sharpmap.dll you downloaded before.

Nov 25, 2012 at 10:57 AM

I am still unsure of what to actually do with the source code for PostGIS.cs that i downloaded from SharpMap100480\Trunk\SharpMap.Extensions\Data\Providers.

I have removed any reference to SharpMap and SharpMap.Extensions and re-added them. I am still getting the same error. Is there anyway of checking the code of the PostGis data provider I am using in my SharpMap.Extensions.dll  does in deed have thefollowing line of code to handle my error

if (_postGisVersion >= 1.5) _prefixFunction = "ST_";

Or if someone could link me to the most up to date SharMap.Extensions.dll that definately has the right PostGIS provider for my problem

Thank You

Developer
Nov 25, 2012 at 2:22 PM
You added the Sharpmap and Sharpmap Extensions projects to your solution and set the references to these projects (don't set the references to de dll)? It seems to me something went wrong there, because you shouldn't get this error anymore. I have developed with SharpMap and Postgis2.x and this workes fine for me. But I always work with the SharpMap source code in my projects, so thats why I advice you to do the same.
Nov 25, 2012 at 3:47 PM

Thanks for your replies PeaceNlove I suppose my problem then is how do i work with sharpmap using source code alone. if you could walk me step by step through how to set it up in visual studio i would be very greatful.

Thank you

Developer
Nov 25, 2012 at 3:57 PM
Please send me an email @ hempenius AT gmail Dotcom, I can send you some screenshots from my VS2010 to show you how to do it.


On Sun, Nov 25, 2012 at 5:48 PM, sim1986 <notifications@codeplex.com> wrote:

From: sim1986

Thanks for your replies PeaceNlove I suppose my problem then is how do i work with sharpmap using source code alone. if you could walk me step by step through how to set it up in visual studio i would be very greatful.

Thank you

Read the full discussion online.

To add a post to this discussion, reply to this email (SharpMap@discussions.codeplex.com)

To start a new discussion for this project, email SharpMap@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com




--
Dorbeendreef 174
3561KN Utrecht
+31 6 18652455
Nov 25, 2012 at 4:26 PM

I have sent you an email

Thanks again for your help