Beispiel #1
0
        public static void SetCoordinate(this OSGeo.OGR.Geometry geometry, int index, ICoordinate coordinate)
        {
            if (geometry != null && coordinate != null)
            {
                if (index >= 0 && index < geometry.GetPointCount())
                {
                    int dimension = geometry.GetCoordinateDimension();
                    switch (dimension)
                    {
                    case 2:
                        geometry.SetPoint_2D(index, coordinate.X, coordinate.Y);
                        break;

                    case 3:
                        geometry.SetPoint(index, coordinate.X, coordinate.Y, coordinate.Z);
                        break;

                    case 4:
                        geometry.SetPointZM(index, coordinate.X, coordinate.Y, coordinate.Z, coordinate.M);
                        break;
                    }
                }
            }
        }
        public static bool ExportSelectedDatabaseToGML(Action <string, bool> aLog, string pOutputFilename)
        {
            // Get database, return if no database selected
            if (frmMain.dbx == null)
            {
                aLog("Please select a database first...", true);
                return(false);
            }

            // Setup driver
            OSGeo.OGR.Driver drv = Ogr.GetDriverByName("GML");
            if (drv == null)
            {
                aLog("Could not load driver", true);
                return(false);
            }

            // Create fieldAttributes datasource
            DataSource ds = drv.CreateDataSource(pOutputFilename, null);

            if (ds == null)
            {
                aLog("Could not create datasource", true);
                return(false);
            }

            // Create fieldAttributes layer
            OSGeo.OGR.Layer l = ds.CreateLayer("AddressUnits", null, wkbGeometryType.wkbPoint, null);
            if (l == null)
            {
                aLog("Failed to create GML file: AddressUnits", true);
                return(false);
            }

            // Create fieldAttributes class to hold address unit data
            AddressUnit mAddressUnits = new AddressUnit();

            // Add fields to shapefile
            foreach (System.Reflection.FieldInfo mFld in mAddressUnits.GetType().GetFields())
            {
                var val       = (DatFld)mFld.GetValue(mAddressUnits);
                var mNewField = new FieldDefn(mFld.Name, val.type);
                if (val.type == FieldType.OFTString)
                {
                    mNewField.SetWidth(val.length);
                }
                if (Ogr.OGRERR_NONE != l.CreateField(mNewField, 1))
                {
                    aLog("Failed to add field: " + mFld.Name, true);
                }
            }

            int    ctr = 0;
            double mX, mY;

            var mAdmAdrFeature = new AddressUnitFeature(frmMain.dbx);

            DataTable mTable = mAdmAdrFeature.GetTable(sqlStatements.selectAddressUnitsSQL);

            // Add features
            foreach (DataRow mRow in mTable.Rows)
            {
                if (Double.TryParse(mRow["loc_x"].ToString(), out mX) && Double.TryParse(mRow["loc_y"].ToString(), out mY))
                {
                    var mFeature = new OSGeo.OGR.Feature(l.GetLayerDefn());
                    var mPoint   = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPoint);

                    mPoint.SetPoint(0, mX, mY, 0);
                    mFeature.SetFID(ctr);
                    mFeature.SetGeometry(mPoint);
                    mFeature.SetField("ADDRESSUNITID", int.Parse(mRow["id"].ToString()));
                    mFeature.SetField("ROADID", int.Parse(mRow["road_id"].ToString()));
                    mFeature.SetField("ADDRESSUNITNR", int.Parse(mRow["addressUnitNumber"].ToString()));
                    mFeature.SetField("ROADNAME_EN", Utilities.GetANSI(mRow["NAMEENGLISH"].ToString()));
                    mFeature.SetField("ROADNAME_AR", Utilities.GetANSI(mRow["NAMEARABIC"].ToString()));
                    mFeature.SetField("DISTRICT_EN", Utilities.GetANSI(mRow["DISTRICT_EN"].ToString()));
                    mFeature.SetField("DISTRICT_AR", Utilities.GetANSI(mRow["DISTRICT_AR"].ToString()));
                    mFeature.SetField("MUNICIPALITY_EN", Utilities.GetANSI(Utilities.LABEL_ABUDHABI_EN));
                    mFeature.SetField("MUNICIPALITY_AR", Utilities.GetANSI(Utilities.LABEL_ABUDHABI_AR));
                    mFeature.SetField("QR_CODE", Utilities.GetANSI(mAdmAdrFeature.GetQRCode(mRow, mTable)));
                    l.CreateFeature(mFeature);
                }
                else
                {
                    Utilities.LogDebug("Error");
                }

                ctr++;
            }

            l.Dispose();
            ds.Dispose();
            drv.Dispose();
            return(true);
        }