Dear Technical support,
I am trying to have a feature layer from sql database rather than the shape file’s feature layer. Can you give me a sample code that takes feature layer from database?
Thanks.
Using custom feature layer from sql database in thinkgeo web edition
Hi Samuel,
Thanks for evaluating Map Suite products, displaying data from a sql server database is one feature of map suite, it can be easily used, just simply as shape file, please check the demo code as following:
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
winformsMap1.CurrentExtent =
new
RectangleShape(-126.4, 48.8, -67.0, 19.0);
winformsMap1.BackgroundOverlay.BackgroundBrush =
new
GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
string
connectString =
"Data Source=HOST;Initial Catalog=YOUR DATABASE;Persist Security Info=True;User ID=USER NAME;Password=USER PASSWORD"
;
MsSql2008FeatureLayer sql2008Layer =
new
MsSql2008FeatureLayer(connectString,
"YOUR TABLE NAME/VIEW NAME"
,
"RECORD ID"
);
sql2008Layer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
sql2008Layer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
sql2008Layer.Open();
RectangleShape rect = sql2008Layer.GetBoundingBox();
LayerOverlay staticOverlay =
new
LayerOverlay();
staticOverlay.Layers.Add(
"Sql2008Layer"
, sql2008Layer);
winformsMap1.Overlays.Add(staticOverlay);
winformsMap1.Refresh();
Thanks,
Johnny
Thank you for your prompt response. This is nice.
I have used the "HookUpExternalDataToShapefile" concept to link external data with my shape file. I am using the demographic and life style maps template in the web edition. I have replaced the shape file with a district level shape file for Africa. The .dbf file have 4120 records. When I try to run, its giving me System.OutOfMemoryException. My laptop has a RAM of 8GB. So could you any possible solution for this.
Thanks in advance.
Here is the stack trace
[OutOfMemoryException: Exception of type ‘System.OutOfMemoryException’ was thrown.]
System.Text.StringBuilder.ToString() +35
System.String.Format(IFormatProvider provider, String format, Object[] args) +72
ThinkGeo.MapSuite.WebEdition.Map.4RU=() +611
ThinkGeo.MapSuite.WebEdition.Map.RenderChildren(HtmlTextWriter writer) +192
System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) +10
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +32
ThinkGeo.MapSuite.WebEdition.Map.Render(HtmlTextWriter writer) +948
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128
System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +216
System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32
System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +53
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1386
Hi Samuel,
Could you please send your demo to forumsupport@thinkgeo.com for our test? As we are unable to recreate it with your description.
Thanks,
Johnny
Here is the partial code
private void InitializeMapControl()
{
Map1.MapUnit = GeographyUnit.DecimalDegree;
Map1.MapTools.Logo.Enabled = false;
Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
Map1.CurrentExtent = new RectangleShape(-28.17864375, 36.9286546875, 67.11614375, -37.1903734375);
// base map layers
WorldMapKitWmsWebOverlay worldMapKitOverlay = new WorldMapKitWmsWebOverlay();
Map1.CustomOverlays.Add(worldMapKitOverlay);
worldMapKitOverlay.Name = "Base Layer";
Map1.MapTools.OverlaySwitcher.Enabled = true;
CustomDataShapeFileFeatureLayer statesLayer = GetCustomDataShapeFileFeatureLayer();
ThematicDemographicStyleBuilder selectedStyle = new ThematicDemographicStyleBuilder(new Collection<string>() { "EndemicityLF" });
statesLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(selectedStyle.GetStyle(statesLayer.FeatureSource));
statesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
Map1.DynamicOverlay.Layers.Add("usStatesLayer", statesLayer);
// highlight layers
Map1.HighlightOverlay.HighlightStyle = new FeatureOverlayStyle(GeoColor.FromArgb(150, GeoColor.FromHtml("#449FBC")), GeoColor.FromHtml("#014576"), 1);
Map1.HighlightOverlay.Style = new FeatureOverlayStyle(GeoColor.SimpleColors.Transparent, GeoColor.SimpleColors.Transparent, 0);
statesLayer.Open();
foreach (Feature feature in statesLayer.FeatureSource.GetAllFeatures(ReturningColumnsType.NoColumns))
{
Map1.HighlightOverlay.Features.Add(feature.Id, feature);
}
statesLayer.Close();
// Add hover-popup
CloudPopup featureinfoPopup = new CloudPopup("featureInfoPopup", new PointShape(-300, -200), "CNTRY_NAME"); // make the popup out of map view temporarily
featureinfoPopup.AutoSize = true;
Map1.Popups.Add(featureinfoPopup);
// Add Legend adorment overlay
LegendAdornmentLayer legendAdornmentLayer = new LegendAdornmentLayer();
legendAdornmentLayer.Location = AdornmentLocation.LowerLeft;
legendAdornmentLayer.XOffsetInPixel = 15;
legendAdornmentLayer.Title = new LegendItem();
legendAdornmentLayer.Title.ImageJustificationMode = LegendImageJustificationMode.JustifyImageRight;
legendAdornmentLayer.Title.TopPadding = 10;
legendAdornmentLayer.Title.BottomPadding = 10;
legendAdornmentLayer.Title.TextStyle = new TextStyle("EndemicityLF", new GeoFont("Segoe UI", 12), new GeoSolidBrush(GeoColor.SimpleColors.Black));
Map1.AdornmentOverlay.Layers.Add(legendAdornmentLayer);
ThematicDemographicStyleBuilder initDemographicStyle = new ThematicDemographicStyleBuilder(new Collection<string>() { "EndemicityLF" });
UpdateLegend(initDemographicStyle);
}
public CustomDataShapeFileFeatureLayer GetCustomDataShapeFileFeatureLayer()
{
CustomDataShapeFileFeatureLayer USStatesLayer = new CustomDataShapeFileFeatureLayer(Server.MapPath(ConfigurationManager.AppSettings["AfricaShapefilePath"]));
USStatesLayer.InitDictionaries("CNTRY_NAME", GetTestDataTable(), "EndemicityLF","EndemicityOncho","EndemicitySTH","EndemicitySCH","EAPreS","EAS","Adults");
//USStatesLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
//Get the cloumn of the database.
//USStatesLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.Country1("Population");
//USStatesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
return USStatesLayer;
}
I tried to upload the shape file but it is not being uploaded. So I am sharing it using google drive.
here is the link.
drive.google.com/file/d/0B34usSRQSHLieGJxOEtEQjRpcEE/view?usp=sharing
Hi Samuel.
Seems that there are something wrong with columns in the code as following:
USStatesLayer.InitDictionaries(“CNTRY_NAME”,
GetTestDataTable(), “EndemicityLF”,“EndemicityOncho”,“EndemicitySTH”,“EndemicitySCH”,“EAPreS”,“EAS”,“Adults”);
From the shapefile you provided, the columns are LVLID, LVLID_3, ISO_CTRY, ISO_3, STATECODE,
NAME, RECID, seems like the “LinkColumn” is missed. thus could you please provide us a project for test? If it’s a bit big, please send it to forumsupport@thinkgeo.com.
Thanks,
Johnny
Hi Johnny,I have sent the project file along with the SQL script to forumsupport@thinkgeo.com.
regards,
Samuel
Hi Samuel,
Thanks for your demo. The “HighlightOverlay” is a lightweight overlay for displaying feature on client side. In other words, the features included in the “HighlightOverlay” are serialized into JSON format and then send it to client side for render using SVG or HTML5 Canvas, thus we are unable to add a huge data to this overlay, as they need to be sent to client side via HTTP, it will slow down the internet, maybe runs into timeout problem. Also it’s hard for browser to draw these huge data, the browser may crash.
When we debug the project you provided, seems like there are 4190 features queried from shapefile and SQL Server, after serialized into JSON, it’s 88M bytes. I guess that’s the problem, is it possible for you guys showing these features with LayerOverlay or do some simplification to these features?Could you let us your requirements?
Thanks,
Johnny
Dear Johnny,
The problem is as you explained. Too many features to display. Which ever way I need to have the district level map linked with external SQL database for feature data. So the mathed doest matter. I am just looking for possible approaches.
Thanks
That’s ok, Samuel. Any help required please feel free to let us know.
Thanks,
Johnny
Hi Johnny,
Based on the shapefile and database I shared, could you send me a sample code on how I can accomplish my requirements. District level mapping with custom feature layer from database.
Thanks,
Samuel
Dear Johnny,
I take some time to go through the different overlay types and I think I grasp a good understanding. Now I am looking for the legend to be modified based on the zoom levels. If you could refer me to some code samples I would be grateful.
Regards,
Samuel
Hi Samuel,
The easiest way to accomplish this is transforming the shapefile to MsSql2008FeatureLayer, thus the shapefile will like a table in database. Then we update well-known data to this geometry table. sharpgis.net/page/Shape2SQL is a small but powerful tool for uploading shapefiles to SQL server. After this, we can get shapes and column values from SQL server directly. If it is needed, we also could add this layer to an overlay and add tilecache for overlay to get a better performance.
Hope this would be helpful and any other question please feel free to let us know.
Thanks,
Kevin