ThinkGeo.com    |     Documentation    |     Premium Support

PostgreSql FeatureLayer

 Hi,


      I have setup a map with a PostgreSqlFeatureLayer as the code below, but on clicking a station point no one feature is found. am I doing something wrong ?


Thanks in advance


Paolo Saudin


 



         protected void Page_Load(object sender, EventArgs e)


        {


            if (!Page.IsPostBack)


            {


 


                /*


                 * map properties


                 */


                Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));


                Map1.MapUnit = GeographyUnit.Meter;


 


                /*


                 * map tools


                 */


                Map1.MapTools.OverlaySwitcher.Enabled = true;


                Map1.MapTools.OverlaySwitcher.BackgroundColor = GeoColor.StandardColors.Lavender;


                Map1.MapTools.OverlaySwitcher.BaseOverlayTitle = "Mappa";


                Map1.MapTools.OverlaySwitcher.DynamicOverlayTitle = "Layers";


                Map1.MapTools.PanZoomBar.Enabled = true;


                Map1.MapTools.PanZoomBar.IsGlobeButtonEnabled = false;


                Map1.MapTools.MouseCoordinate.Enabled = true;


                Map1.MapTools.MouseMapTool.Enabled = true;


                Map1.MapTools.ScaleLine.Enabled = true;


 


                /*


                 * main layer


                 */


                GoogleOverlay googleOverlay = new GoogleOverlay("GoogleOverlay");


                googleOverlay.JavaScriptLibraryUri = new Uri(ConfigurationManager.AppSettings["GoogleUri"]);


                googleOverlay.GoogleMapType = GoogleMapType.Normal;


 


                /*


                 * stations layer


                 */


                PostgreSqlFeatureLayer stationsLayer = null;


                string connectString = "Server=localhost;User Id=postgres;Password=postgres;DataBase=rete_llpp;";


                stationsLayer = new PostgreSqlFeatureLayer(connectString, "metadati_postgis", "st_id", 23032, "metadata", "geometria");


                stationsLayer.Name = "Stations Layer";


                stationsLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = new PointStyle(PointSymbolType.Circle, new GeoSolidBrush(GeoColor.SimpleColors.DarkBlue), 9);


                stationsLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level12;


 


                /*


                 * manage the projection


                 */


                Proj4Projection proj4 = new Proj4Projection();


                proj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(23032);


                proj4.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();


                


                /*


                 * create overlays


                 */


                // stations


                LayerOverlay stationsOverlay = null;


                if (stationsLayer != null)


                {


                    stationsOverlay = new LayerOverlay("Stations Overlay", false, TileType.SingleTile); // , false, TileType.SingleTile


                    stationsOverlay.Layers.Add("Stations Layer", stationsLayer);


                    stationsOverlay.TransitionEffect = TransitionEffect.None;


                    stationsLayer.FeatureSource.Projection = proj4;


                }


 


                /*


                 * finally add all the layers


                 */


                if (googleOverlay != null) { Map1.CustomOverlays.Add(googleOverlay); }


                if (stationsOverlay != null) { Map1.CustomOverlays.Add(stationsOverlay); }


 


                /*


                 * sets the extents


                 */


                stationsLayer.Open();


                Map1.CurrentExtent = stationsLayer.GetBoundingBox();


                stationsLayer.Close();


   


            }


        }


 


        protected void Map1_Click(object sender, MapClickedEventArgs e)


        {


            LayerOverlay staticOverlay = (LayerOverlay)Map1.CustomOverlays["Stations Overlay"];


 


            PostgreSqlFeatureLayer shapeFileLayer = (PostgreSqlFeatureLayer)(staticOverlay.Layers[0]);


 


            PointShape pointShape = (PointShape)e.Position;


            string connectString = "Server=localhost;User Id=postgres;Password=postgres;DataBase=rete_llpp;";


            shapeFileLayer.ConnectionString = connectString;


            shapeFileLayer.Open();


            Collection<Feature> selectedFeatures = shapeFileLayer.QueryTools.GetFeaturesContaining(pointShape, ReturningColumnsType.AllColumns);


            shapeFileLayer.Close();


 


            CloudPopup popup;


            if (Map1.Popups.Count == 0)


            {


                popup = new CloudPopup("Popup", e.Position, string.Empty, 260, 60);


                popup.IsVisible = true;


                Map1.Popups.Add(popup);


            }


            else


            {


                popup = (CloudPopup)Map1.Popups["Popup"];


                popup.Position = e.Position;


            }


 


            popup.ContentHtml = GetPopupContent(selectedFeatures);


        }


 


        private static string GetPopupContent(Collection<Feature> features)


        {


            string content;


            if (features.Count > 0)


            {


                StringBuilder message = new StringBuilder();


                message.AppendFormat("

  • Station Name : {0}
  • ", features[0].ColumnValues["stazione"].Trim());


                    string messageInPopup = String.Format("{0}", message.ToString());


                    content = messageInPopup;


                }


                else


                {


                    content = @"Clicca su di un punto per ottenere le informazioni.";


                }


                return content;


            }


     


     



     


    Paolo,
    I think the problem comes from the line of code “Collection selectedFeatures = shapeFileLayer.QueryTools.GetFeaturesContaining(pointShape, ReturningColumnsType.AllColumns);”. When we click on the map, the application just can get a point where we click, it should a coordinate (X, Y), but the stations also are presented by (X, Y) coordinates. I think it’s hard to say one point contains another point without any buffer, right? So please use “shapeFileLayer.QueryTools.GetFeaturesNearestTo” instead.
     
    Thanks,
     
    Johnny

    Hi Johnny, 
      
       it works like a charm !! I’am really new to gis stuff … 
        
     Thank you very much !! 
     paolo

    Hi Paolo, 
      
     That’s great. Any questions please let us know. 
      
     Thanks, 
      
     Johnny