How to create a polygon by coordinates?

There is a set of coordinates in the form of JSON from MapInfo. I used to use this in EzGIS to create polygons and a whole layer of geometry in the database. But in MapWinGIS everything is different. Can a small example of how this can be used? Here is a set of data… coordinate points and n-parts for the polygon.
{

"nparts": [0,5],

"coord": [

{"lon":5893106.41,"lat":539612.01},

{"lon":5893112.34,"lat":539549.55},

{"lon":5893129.83,"lat":539551.27},

{"lon":5893123.89,"lat":539613.74},

{"lon":5893106.41,"lat":539612.01},

{"lon":5893094.36,"lat":539674.39},

{"lon":5893100.07,"lat":539611.8},

{"lon":5893117.32,"lat":539613.32},

{"lon":5893111.72,"lat":539675.92},

{"lon":5893094.36,"lat":539674.39}

]

}

All the same, anyone tell me example how to create a polygon? The examples in the documentation create circles and random. And I need a rectangle or a polygon at specific points.

It would be something like the following (in c#):

        // initialize a polygon shape
        Shape shp = new Shape();
        shp.Create(ShpfileType.SHP_POLYGON);
        // iterate and add points
        // loop through your points...
        {
            double lon, lat;
            // set to values from your JSON file
            shp.AddPoint(lon, lat);
        }
        // if it's not already, make sure last point is same as first point
        shp.AddPoint(shp.Point[0].x, shp.Point[0].y);

Adding shapes to a shapefile is similar:

        // create a new shapefile (or you can open an existing)
        Shapefile sf = new Shapefile();
        sf.CreateNew("", ShpfileType.SHP_POLYGON);
        if (sf.StartEditingShapes())
        {
            // add your polygons
            sf.EditAddShape(shp);
        }
        sf.StopEditingShapes();

Thanks, I understand. And what should I do with the values of nparts [0,5] they are in MapInfo format. And also, if you make the deserialization of the polygon in MapWinGIS - it looks like this:

15;0;5;
539612.013513|5893106.405287|0.000000|
539549.551243|5893112.344078|0.000000|
539551.274624|5893129.830519|0.000000|
539613.736895|5893123.891728|0.000000|
539612.013513|5893106.405287|0.000000|
539674.393325|5893094.357739|0.000000|
539611.799121|5893100.066577|0.000000|
539613.324602|5893117.323065|0.000000|
539675.918806|5893111.724205|0.000000|
539674.393325|5893094.357739|0.000000|

Nparts values appear if the object consists of two polygons, but they are combined. And when selected polygon, they are one whole. Nparts - I think it’s part of the polygon.

If you know where (at which point) each part starts, you would then call

shp.InsertPart(pointIndex, partIndex)

Presumably, the first part starts at the first point. This would be part 0 (zero).
In your sample data, it looks like maybe the 5th point down is the same point as the first point. Perhaps that’s the start of the second part. And perhaps that’s the interpretation of 0, 5. Point 0 is the start of the , first part, and point 5 is the start of the second part.

So, and I’m guessing a little, you would add your points, then call

shp.InsertPart(0, 0);  // point zero is the start of part 0
shp.InsertPart(5, 1)   // point 5 is the start of part 1

Thanks so much. :+1::+1::+1:
I will try. But in General I understood everything. The case remained in the practice

Can’t you just convert your whole MapInfo file to Shapefile using gdalUtils.GdalVectorTranslate.

If you share your file I can have a look if needed.

1 Like

This topic was automatically closed after 14 days. New replies are no longer allowed.