예제 #1
0
        /// <summary>
        /// Convert the first PostGis Geometry column or the column specified by geomOrdinal into the corresponding NetTopologyGeometry
        /// Can throw the following Exceptions
        /// - ArgumentNullException
        /// - InvalidCastException in case of SRID not compatible for example.
        /// - NotImplementedException if no converter is avaialble for this type.
        /// </summary>
        /// <param name="pgCursor">The cursor where to look for the Geometry column</param>
        /// <param name="geomOrdinal">optionnal, geom. column index if known, otherwise will be search for.</param>
        /// <returns>the NetTopology geometry equivalent.</returns>
        public static Geometry ConvertFrom(IFieldValueGetter pgCursor, int?geomOrdinal = null)
        {
            Geometry retour = null;

            if (pgCursor != null)
            {
                PostgisGeometry geometry = pgCursor.GetFieldValue <PostgisGeometry>(GetGeomtryColumnIndex(pgCursor, geomOrdinal));
                if (geometry is PostgisPoint)
                {
                    retour = ProcessPoint((PostgisPoint)geometry);
                }
                else if (geometry is PostgisMultiPoint)
                {
                    retour = ProcessMultiPoint((PostgisMultiPoint)geometry);
                }
                else if (geometry is PostgisLineString)
                {
                    retour = ProcessLineString((PostgisLineString)geometry);
                }
                else if (geometry is PostgisMultiLineString)
                {
                    retour = ProcessMultiLineString((PostgisMultiLineString)geometry);
                }
                else if (geometry is PostgisPolygon)
                {
                    retour = ProcessPolygon((PostgisPolygon)geometry);
                }
                else if (geometry is PostgisMultiPolygon)
                {
                    retour = ProcessMultiPolygon((PostgisMultiPolygon)geometry);
                }
                else
                {
                    throw new NotImplementedException();
                }
                SetSRID(retour, geometry);
            }
            else
            {
                throw new ArgumentNullException();
            }
            return(retour);
        }
예제 #2
0
 /// <summary>
 /// 1- Check, if geomOrdinal is not null, it is corresponding to a Geom column type. Throw InvalidCastException if type is not geometry or  IndexOutOfRangeException if not in column's range.
 /// 2- Else, iterate from 0 to end the cursor columns and return as soon as a geometry column is found. If no column exists throw ArgumentOutOfRangeException
 /// Throw ArgumentNullException if any of input marameter is null.
 /// </summary>
 /// <param name="pgCursor">a DB Cursor where to search a geometry column. Must be compatible with PostGIS.</param>
 /// <param name="geomOrdinal">if not null, use to convert directly this field into NetTopologyGeometry, otherwise look for column indice one to the end.</param>
 /// <returns>the index of geometry column in the cursor.</returns>
 private static int GetGeomtryColumnIndex(IFieldValueGetter pgCursor, int?geomOrdinal)
 {
     if (pgCursor != null)
     {
         int cursorFieldCount = pgCursor.FieldCount;
         //1-
         if (geomOrdinal != null)
         {
             if (geomOrdinal >= 0 && geomOrdinal < cursorFieldCount)
             {
                 if (IsGeomColumn(pgCursor.GetPostgresType(geomOrdinal.Value)))
                 {
                     return(geomOrdinal.Value);
                 }
                 else
                 {
                     throw new InvalidCastException();
                 }
             }
             else
             {
                 throw new ArgumentOutOfRangeException();
             }
         }
         //2-
         else
         {
             for (int i = 0; i < cursorFieldCount; i++)
             {
                 if (IsGeomColumn(pgCursor.GetPostgresType(i)))
                 {
                     return(i);
                 }
             }
             throw new ArgumentOutOfRangeException();
         }
     }
     else
     {
         throw new ArgumentNullException();
     }
 }