6

How do I extract all the points from a Polygon drawn in ArcMAP and then persist that data into a SQLServer database table containing a geometry column and then when I've got the data persisted read from the database and draw the polygon again.

I'm working with ArcGIS 10 sp1 using ArcObjects for .NET (C#) sp1. I've made an add-in tool to draw the polygon and want that add-in to persist it too. I know how to do the last steps e.g putting the geometry data into the database and drawing an IPolygon instance in ArcMAP, what I don't know how to do is to somehow "convert" the Polygon into something that will "fit" into the geometry column of the database and vice versa.

I'm completely new to ArcObjects programming and GIS systems in general, started two weeks ago, so bare with me...

4
  • Update: I think I found an answer myself. Casting IPolygon to IPointCollection will give access to an enumeration of points, which then is used to build a string to be put into the database...
    – Claus_L
    Commented Sep 23, 2011 at 11:30
  • A product called ArcSDE might make this easier, do you have a license for that? Commented Sep 23, 2011 at 12:57
  • @Kirk - No don't have a license for ArcSDE.
    – Claus_L
    Commented Sep 27, 2011 at 7:39
  • @Kirk - turns out we do have a license for ArcSDE...will check that out too.
    – Claus_L
    Commented Sep 27, 2011 at 8:02

1 Answer 1

6

Your comment adds more insight as to what you are trying to do. You could just store it as WKT (well known text) Use ArcObjects to get the geometry to WKB, then use something like Sharpmap to convert to WKT.

    /// <summary>
    /// Converts geometry into WKB
    /// </summary>
    /// <param name="geometry">The geometry.</param>
    /// <returns>byte array of a WKB geometry</returns>
    public static byte[] ToWKB(this IGeometry geometry)
    {
        IWkb wkb = geometry as IWkb;
        ITopologicalOperator oper = geometry as ITopologicalOperator;
        oper.Simplify();

        IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3;
        byte[] b = factory.CreateWkbVariantFromGeometry(geometry) as byte[];
        return b;
    }


    /// <summary>
    /// Converts WKB to WKT
    /// </summary>
    /// <param name="geometry">The geometry.</param>
    /// <returns>WKT string</returns>
    public static string ToWKT(this IGeometry geometry)
    {
        byte[] wkb = geometry.ToWKB();
        SharpMap.Geometries.IGeometry sharpGeom = SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse(wkb);
        return SharpMap.Converters.WellKnownText.GeometryToWKT.Write(sharpGeom);
    }

    /// <summary>
    /// Sets the byte value.
    /// </summary>
    /// <param name="row">The row to the featureclass or table row</param>
    /// <param name="fieldName">Name of the field.</param>
    /// <param name="data">The byte array to store.</param>
    /// <returns>true if the field is found and value set</returns>
    public static bool SetByteValue(this IRow row, string fieldName, byte[] data)
    {
        int index = row.Fields.FindField(fieldName);

        if (index != -1)
        {
            IMemoryBlobStreamVariant memoryBlobStream = new MemoryBlobStreamClass();
            memoryBlobStream.ImportFromVariant(data);

            if (row.Fields.get_Field(index).Editable)
            {
                row.set_Value(index, memoryBlobStream);
                return true;
            }
        }

        return false;
    }
2
  • @Claus_L: anything you put into a text field won't be spatially indexed. So if you do have ArcSDE, then you'll want to use that (you can instruct ArcSDE to use the native geometry type). Commented Sep 27, 2011 at 11:36
  • Once I have the WKT I plan on using an SQL insert resembling this: INSERT INTO SpatialTable (GeomCol1) VALUES (geometry::STGeomFromText('POLYGON ((0 0, 150 0, 150 150, 0 150, 0 0))', 0)); Changing the last '0' to a suitable SRID... Haven't had the time to find further information on ArcSDE and how to use it...
    – Claus_L
    Commented Sep 27, 2011 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.