Read fields, values and feature points from a .shp example

Jan 19, 2016 at 9:02 PM
Hello,
I was wondering if somebody could provide a MapWindow C# example on how .shp files' fields, values and feature points can be read from it, and printed to a console?
There are C# MapWindow examples, but none of them contains this kind of example.

Thank you for the help in advance.
Coordinator
Jan 21, 2016 at 12:48 PM
You were at the right location when you looked in the API documentation.
Going to the specific function will also reveil some more info.
A shapefile has shapes and fields. A field has columns and a shape has points.

Here's some info about the field class: http://www.mapwindow.org/documentation/mapwingis4.9/class_field.html#details
Using NumShapes you can loop through all shapes in a shapefile: http://www.mapwindow.org/documentation/mapwingis4.9/class_shapefile.html#a8c16abfdf418de3ae93475953958e907
With NumPoints you can loop through all points in a shape: http://www.mapwindow.org/documentation/mapwingis4.9/class_shape.html#af196c875556f1327cfadd5a3effa6ce4

Some pseudocode:
-- Open shapefile
Shapefile.Open(filename);
-- Loop through shapes:
for (int i=0; i<Shapefile.NumShapes; i++)
{
  shp = Shapefile.get_Shape(i) 
  -- Loop through points:
  for (int j = 0; shp.NumPoints; j++)
  {
    pnt = shp.get_Point(j);
    debug.writeline(pnt.X);
  }
}
-- Loop through fields:
for (int i=0; i<Shapefile.NumFields; i++)
{
  fld = Shapefile.get_Field(i);
  debug.writeline(fld.name);
}
When you also want the value of the shape you can use
Shapefile.get_CellValue (int FieldIndex, int ShapeIndex)
Jan 21, 2016 at 1:12 PM
Thank you Mr. Meems,
How would the transformation of the .shp geometry be done (transformation to some EPSG projection for example)?
Can it be done by transforming the entire .shp file, or it needs to be done by transforming each individual point?
Coordinator
Jan 21, 2016 at 1:58 PM
Jan 21, 2016 at 3:56 PM
Edited Jan 21, 2016 at 3:57 PM
Thank you.

I replicated the upper code to ironpython. However an error emerges on shape file Open method.
I am wondering if sometimes this happens in C# too?

When I call the following ironpython cide:
shpFilePath = "D:/mapwindow/nyad.shp"
shapeFile = MapWinGIS.ShapefileClass()
shapeFile.Open(shpFilePath)
On line 3, an error emerges:
Could not convert argument 0 for call to Open.
Any advice on why is this happening?

I apologize for asking such question not related with C#.
Coordinator
Jan 22, 2016 at 8:21 AM
Are you sure you need the ShapefileClass and not just MapWinGIS.Shapefile: http://www.mapwindow.org/documentation/mapwingis4.9/group__shapefile__management.html#ga20dd5a465b84543916144eb515e9e9fd

Here's an example how to use sf.open():
http://www.mapwindow.org/documentation/mapwingis4.9/_add_category_range_8cs-example.html#a3
You need to pass 'null' as the second argument.

Regards,

Paul
Jan 22, 2016 at 10:25 AM
Edited Jan 22, 2016 at 10:26 AM
Thank you.

When I load the Interop.MapWinGIS.dll file to ironpython, there's no Open function within MapWinGIS.Shapefile. There is only one within MapWinGIS.ShapefileClass.


I tried supplying the "null" as the second argument also ("None" in ironpython):
shpFilePath = "D:/mapwindow/nyad.shp"
shapeFile = MapWinGIS.ShapefileClass()
shapeFile.Open(shpFilePath, None)
But the same error message appears:
Could not convert argument 0 for call to Open.
I also tried found an example of passing an instance of MapWinGIS.ShapefileClass.GlobalCallback as the second argument to MapWinGIS.ShapefileClass().Open function, but did not succeed in replicating it to the ironpython.
Coordinator
Jan 25, 2016 at 6:53 AM
We're almost ready to release a new version of MapWinGIS. This installer will include the interop dlls.
Hopefully you can wait a few more days.