I want to load a Postgis layer, but it takes very long time to finishe loading because of mass data. Now I want to load the layer using a filter. For example, I have a field named "Road_class" in the layer, Road class can be "1", "2", "3", what shall I do if I only load the records with a "Road_class=1".
Or, do you have other solution to resolve the performance problem when loading, map manipulation, etc.
You can hook the DrawingFeatures event of the postgre layer and filter the features in the implement of that event. You can take a look at the code in the HowDoISamples\Feature Layers\StopCertainFeaturesFromDrawing. And you can just modify the code as following in your case:
PostgreSqlFeatureLayer postgreLayer = new PostgreSqlFeatureLayer(connectString, "cntry02", "oid"); postgreLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.Highway1; postgreLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.RequiredColumnNames.Add("Road_class"); postgreLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; postgreLayer.DrawingFeatures += new EventHandler<DrawingFeaturesEventArgs>(postgreLayer_DrawingFeatures);
private void postgreLayer_DrawingFeatures(object sender, DrawingFeaturesEventArgs e) { Collection<Feature> featuresToDrawn = new Collection<Feature>(); foreach (Feature feature in e.FeaturesToDraw) { int roadClass = Convert.ToInt32(feature.ColumnValues["Road_class"], CultureInfo.InvariantCulture); if (roadClass == 1) { featuresToDrawn.Add(feature); } }
e.FeaturesToDraw.Clear(); foreach (Feature feature in featuresToDrawn) { e.FeaturesToDraw.Add(feature); } }
Sun, Thank u. It works. In fact, I want to improve the draw performance by controlling the show/hide some feature at different scale, but it seems that the performance has no difference. I use a road layer with 150000 features[I won’t divide this layer into several layers by the Rd_class]. so I have to load all the features and display them, but I found it’s very slow when zooming or panning.
Could u please help me to improve the performance?
Can you try the following code by using the ValueStyle which is exactly satisfy your Case: private void Form1_Load(object sender, EventArgs e) { winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
winformsMap1.CurrentExtent = new RectangleShape(-126.4, 48.8, -67.0, 19.0); winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
Try following code to see performance enhancement:
Hook up the event for postgre FeatureSource as following way first: ((PostgreSqlFeatureSource)(postgreLayer.FeatureSource)).ExecutingSqlStatement += new EventHandler<ExecutingSqlStatementPostgreSqlFeatureSourceEventArgs>(Form1_ExecutingSqlStatement);
In the event , try to change the sqlstatement which will used to get data from database: void Form1_ExecutingSqlStatement(object sender, ExecutingSqlStatementPostgreSqlFeatureSourceEventArgs e) { if (e.ExcutingSqlStatementType == ExecutingSqlStatementType.GetFeaturesInsideBoundingBox) { string sqlStatement = e.SqlStatement; e.SqlStatement = sqlStatement.TrimEnd(';') + " and Road_class='1';"; } }
Well, now I can use the filter to load the features by a SQL script, but I got the result in overall laye, how can I remove the features outside current view?
I think the resultant features returned should ONLY contains the features within the current view. Basically, you can verify this from the sqlstatement.
In Postgre, the shape data is stored in binary which is different with well known binary, while the function “AsBinary” provided can be used to get well known binary from it. In reverse, the function “GeomFromWKB” provided can be used to transfer well known binary to binary stored in Postgre.
Select AsBinary("shape") from austinstreets Select GeomFromWKB(AsBinary("shape") ) from austinstreets
About the exception happening, there are 2 ways to solve the problem: 1) Use single thread instead of multi threaded as following way: winformsMap1.ThreadingMode = MapThreadingMode.SingleThreaded;
2) Use the following code to execute code in main thread instead of drawing thread. private delegate void ToUIThreadDelegate(ExecutingSqlStatementPostgreSqlFeatureSourceEventArgs e);
And all I have done is just want to improve the performance when paning/zooming etc. I have tried many approaches: create spatial index, loading data using a filter, dispaly feature at certain sacle. well, the performance is improved, but not as smooth as in Q-gis or uDig. Do u have some suggestion to do it better.