ThinkGeo.com    |     Blog    |     Wiki    |     Support

(Spatial Binning) How to change many points, with value attribute, belonging to the same polygon, into one point with attribute being arithmetic average from attributes?

I have thousands of points with the value attribute, I would like to divide the area into a grid, and in any case, when more than one point belongs to the mesh, replace it with a single point with the average value of the attributes from the points that were qualified for that mesh. What should I use the available methods in ThinkGeo?

Hi Piotr,

I think ClusterPointStyle should works for it.

If you don’t want to use this style, I build a simple sample which follow your requirement, you can modify it to make it works for your scenario.

9627.zip (12.7 KB)

Wish that’s helpful.

Regards,

Ethan

Hi Ethan, thank you for example, in my case I come from serviceseditionsample_isolineswithkriging_cs_120914, where we have:
//Greate the grid definition based on the extent, cell size etc. GridDefinition gridDefinition = new GridDefinition(currentDrawingExtent, cellSize, -9999, WellDepthPointData);
The problem is that a few data can go into the same mesh and I would like to change it into one point.
In your example you build region manually - how to get regions from gridDefinition? If I have to pass points collection - is it mean that probably I can have access to file of points belonging to the particular mesh - am I right? (b.t.w. GridFeatureSource.GenerateGrid like to freeze, is there any data processing indicator)?

Hi Piotr,

I don’t understand your question this time, could you please describe it more detail?

The GridFeatureLayer can generate grid file which contains many grid shapes, I think it’s not the same like cluster, here we have a sample about it: https://github.com/ThinkGeo/CreateGRIDSample-ForWinForms

Regards,

Ethan

How can I take each cell in the created GridDefinition and operate on values belong to points from this cell? What is it for? I have a lot of samples of Radio Signal Level and there are duplicates data taken from different base station - I have to have a look at each cell and do some operation - somtimes it will be replaceing few points to one and somtimes I want to remove particular point. All these operation I want to do before interpolation process.

Hi Piotr,

Here is some code to get cell rectangle and its cell value.

            winformsMap1.MapUnit = GeographyUnit.DecimalDegree;

        GridDefinition gd = new GridDefinition(new RectangleShape(-100, 100, 100, -100), 5, -9999, new System.Collections.Generic.Dictionary<PointShape, double>());

        using (FileStream stream = new FileStream("test.grid", FileMode.CreateNew))
        {
            GridFeatureLayer.GenerateGrid(gd, new InverseDistanceWeightedGridInterpolationModel(), stream);
        }

        GridFeatureLayer layer = new GridFeatureLayer("test.grid");
        layer.Open();

        Collection<Feature> fs = layer.FeatureSource.GetAllFeatures(ReturningColumnsType.AllColumns);
        foreach (Feature f in fs)
        {
            RectangleShape rect = f.GetBoundingBox();
            string cellValue = f.ColumnValues["CellValue"];
        }

I think your data points should be passed into the GridDefinition, and if you replace the points or remove point, you should want to regenerate the grid file.

The grid file looks like this:

image

Regards,

Ethan

Hi Ethan - good point but I have to make order in the particluar cell before grid generation, input data some times are so badly that the process of interpolation cannot hadle it and I have exception like this one:thinkgeoErrGridGen . Maybe I should know how to check data before first generation of grid file, and then go your way?

Piotr

Hi Piotr,

Could you please upload a sample data so I can see where is the problem?

From your exception it looks your data contains the points with same coordination, you can just loop all points and remove duplication ones.

Regards,

Ethan

GridFeatureSource.GenerateGrid
9.0.0.0
ThinkGeo.MapSuite.Core.LinealKrigingGridInterpolationModel
Referencing Point Count:5
OpenStreetMap Context:2064774.022246,7223981.02799439,2068710.51935624,7220794.56734811
Cell Size:39,8307580784662, no-data value:-9999
Data Points:
2066962.1599725,7222854.45889765,-50.1763000488281
2066966.16747417,7222856.36550233,-50.4888000488281
2066949.02427259,7222887.25256258,-57.2349014282227

OK, so I have to remove dupilicates, next issue is about the above data - function GridFeatureSource.GenerateGrid never ends.

Hi Piotr,

I don’t know why it never ends, could you please upload a sample project with test data, so we can see where is the problem?

Because the GridFeatureLayer is rarely used, so we don’t have more experience about it, our developer need debug to look into it.

Regards,

Ethan

Hi Ethan - it base on your exmaple - just run it with these 3 points and give it 5 reference points (background map is OSM - that’s why the points are not in WGS84).

Hi Ethan - ThinkGeo as a company does make any unit test on other windows culture settings?
I work with both examples: serviceseditionsample_isolines_cs_winforms_111115 and serviceseditionsample_isolineswithkriging_cs_120914 and it looks like you never ever run it under any Windows internation setting having in decimal coma. This is not only problem with examples - that I can fix. For example parsing the numbers change: double.Parse(parts[0],CultureInfo.InvariantCulture) but if you introduce in program.cs (.NET framework 4.62):
var culture = CultureInfo.GetCultureInfo(“pl-PL”);
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

(this is more easy to do then change Windows international settings)

The reading of the gridfile by function GetGridFeatureLayer() result in no features at all!

Hi Piotr,

So does the culture problem caused the never end issue?

Our sample mainly shows how to use the APIs, so it maybe have some problem, but in our code it handle the culture.

In our inner logic, the data always be InvariantCulture, if any inner data be converted to string, it also will be handled as CultureInfo.InvariantCulture, if we convert a string back and we know it’s generated by our logic, we will handle it like Convert.ToDouble(string, CultureInfo.InvariantCulture).

So if your grid file is generated by our code, it should be follow InvariantCulture but not the target culture for Poland, I think there is some problem here, could you please upload your grid file so we can look into it?

In fact we also found the “new GridFeatureLayer(gridFilePath)” can get enhanced, because user maybe pass into a custom grid file(That’s not by design). We will did some test based on your grid file and see how to enhance it.

Regards,

Ethan

Hi Ethan - just take your standard example and add to program.cs the change of Culture from my example - you will see that map would be without colored zones between isolines, please.

Hi Piotr,

Thanks to point that, I download the sample DisplayIsoLinesWinforms and make it run. Then I reproduced the problem you mentioned.

The reason why the DynamicGridFeatureLayer don’t render is because the test data in “GrayCountyIrrigationWellDepths.csv” follow CultureInfo.InvariantCulture, but in the ClassBreakStyle it tried to convert the data into CultureInfo.CurrentCulture. So if you set culture in Program.cs, it will try to convert the data to “xxx,xxx”, after convert failed it won’t render any feature(grids).

So I think you should want to add these lines into program.cs:
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

And if your data follow any Culture, you need to set it in Sample.cs when load the data(For example in this line wellDataPoints.Add…), then it should works well.

Wish that’s helpful.

Regards,

Ethan

Hi Ethan the problem is not only in reading GrayCountyIrrigationWellDepths.csv - that was fixed by me by parser argument change. Your solution has big impact on the rest of the application - dialogs for open file, error communication and all inormation having source in OS system. I think that your internal procedures are culture depended.

Hi Piotr,

I modify the sample, it provide another solution, it convert the culture in GetFeaturesInsideBoundingBoxCore function of the class DynamicGridFeatureSource, and it works even you set culture in program.cs.

DisplayIsoLinesWinforms.zip (104.5 KB) It’s for version 9.

Wish that’s helpful.

And our development team will discuss about how to handle the possible problem in culture, it looks our old logic still will met problem in special scenario. Thanks for let us know it.

Regards,

Ethan

Thank you Ethan - it works, we can close this thread.

Hi Piotr,

Thanks for your update.

Regards,

Ethan