Beispiel #1
0
        public static void GenerateParkingRegionShape(List <MBR> mbrs, string fileName, bool addDev = false)
        {
            String rootDir       = Path.GetDirectoryName(fileName);
            String shapeFileName = Path.GetFileNameWithoutExtension(fileName);
            var    transform     = new Wgs2MgsTransform();
            // Read txt
            ShapeType shapeType = ShapeType.PolyLine;

            DbfFieldDesc[] shpFields = new DbfFieldDesc[]
            {
                new DbfFieldDesc {
                    FieldName = "ID", FieldType = DbfFieldType.Character, FieldLength = 14, RecordOffset = 0
                },
                //new DbfFieldDesc { FieldName = "Name", FieldType = DbfFieldType.Character, FieldLength = 18, RecordOffset = 14 },
            };
            ShapeFileWriter sfw = ShapeFileWriter.CreateWriter(rootDir, shapeFileName, shapeType, shpFields);

            foreach (var mbr in mbrs)
            {
                String[]      fieldData = new string[] { " " };
                List <PointF> vertices  = new List <PointF>();
                GeoPoint[]    points    = new GeoPoint[] { mbr.TopLeft, mbr.TopRight, mbr.BottomRight, mbr.BottomLeft, mbr.TopLeft };
                for (int i = 0; i < points.Length; ++i)
                {
                    var point = points[i];
                    if (addDev)
                    {
                        point = transform.Transform(point);
                    }
                    vertices.Add(point.ToPointF());
                }
                sfw.AddRecord(vertices.ToArray(), vertices.Count, fieldData);
            }
            sfw.Close();
        }
Beispiel #2
0
        private static void setParkingPointsAsShapeFile(List <GeoPoint> points, string fileName, bool addDev = false)
        {
            //写入文件
            String    rootDir       = Path.GetDirectoryName(fileName);
            String    shapeFileName = Path.GetFileNameWithoutExtension(fileName);
            ShapeType shapeType     = ShapeType.Point;
            var       transform     = new Wgs2MgsTransform();

            DbfFieldDesc[] fields = new DbfFieldDesc[]
            {
                new DbfFieldDesc {
                    FieldName = "ID", FieldType = DbfFieldType.Character, FieldLength = 14, RecordOffset = 0
                },
                //new DbfFieldDesc { FieldName = "Name", FieldType = DbfFieldType.Character, FieldLength = 18, RecordOffset = 14 },
            };
            ShapeFileWriter sfw = ShapeFileWriter.CreateWriter(rootDir, shapeFileName, shapeType, fields);

            foreach (var p in points)
            {
                String[]      fieldData = new string[] { " " };
                List <PointF> vertices  = new List <PointF>();
                GeoPoint      point     = p;
                if (addDev)
                {
                    point = transform.Transform(point);
                }
                float lng = (float)(point.Lng);
                float lat = (float)(point.Lat);
                vertices.Add(new PointF(lng, lat));
                sfw.AddRecord(vertices.ToArray(), vertices.Count, fieldData);
            }
            sfw.Close();
        }
Beispiel #3
0
        public void SaveAsShpFile(String fileName, double offsetLat = 0, double offsetLng = 0)
        {
            //写入文件
            String    rootDir       = Path.GetDirectoryName(fileName);
            String    shapeFileName = Path.GetFileNameWithoutExtension(fileName);
            ShapeType shapeType     = ShapeType.PolyLine;

            DbfFieldDesc[] fields = new DbfFieldDesc[]
            {
                new DbfFieldDesc {
                    FieldName = "ID", FieldType = DbfFieldType.Character, FieldLength = 14, RecordOffset = 0
                },
                //new DbfFieldDesc { FieldName = "Name", FieldType = DbfFieldType.Character, FieldLength = 18, RecordOffset = 14 },
            };
            ShapeFileWriter sfw = ShapeFileWriter.CreateWriter(rootDir, shapeFileName, shapeType, fields);

            foreach (Edge e in Edges.Values)
            {
                String id = e.ID.ToString();
                if (e.ID % 2 == 0)
                {
                    continue;
                }
                String[]      fieldData = new string[] { id };
                List <PointF> vertices  = new List <PointF>();
                for (int i = 0; i < e.Geo.Points.Count; i++)
                {
                    float lng = (float)(e.Geo.Points[i].Lng + offsetLng);
                    float lat = (float)(e.Geo.Points[i].Lat + offsetLat);
                    vertices.Add(new PointF(lng, lat));
                }
                sfw.AddRecord(vertices.ToArray(), vertices.Count, fieldData);
            }
            sfw.Close();
        }
Beispiel #4
0
        private static void setAsShapeFile(Dictionary <long, HashSet <string> > edgeDevicesDict, Graph graph, string fileName, bool addDev = false)
        {
            //写入文件
            String    rootDir       = Path.GetDirectoryName(fileName);
            String    shapeFileName = Path.GetFileNameWithoutExtension(fileName);
            ShapeType shapeType     = ShapeType.PolyLine;
            var       transform     = new Wgs2MgsTransform();
            int       threshold     = 1;

            DbfFieldDesc[] fields = new DbfFieldDesc[]
            {
                new DbfFieldDesc {
                    FieldName = "ID", FieldType = DbfFieldType.Character, FieldLength = 14, RecordOffset = 0
                },
                //new DbfFieldDesc { FieldName = "Name", FieldType = DbfFieldType.Character, FieldLength = 18, RecordOffset = 14 },
            };
            ShapeFileWriter sfw = ShapeFileWriter.CreateWriter(rootDir, shapeFileName, shapeType, fields);

            foreach (var p in edgeDevicesDict)
            //foreach (Edge e in Edges.Values)
            {
                if (edgeDevicesDict.Count < threshold)
                {
                    continue;
                }
                Edge   e  = graph.Edges[p.Key];
                String id = e.ID.ToString();
                if (e.ID % 2 == 0)
                {
                    continue;
                }
                String[]      fieldData = new string[] { "" };
                List <PointF> vertices  = new List <PointF>();
                for (int i = 0; i < e.Geo.Points.Count; i++)
                {
                    GeoPoint point = e.Geo.Points[i];
                    if (addDev)
                    {
                        point = transform.Transform(point);
                    }
                    float lng = (float)(point.Lng);
                    float lat = (float)(point.Lat);
                    vertices.Add(new PointF(lng, lat));
                }
                sfw.AddRecord(vertices.ToArray(), vertices.Count, fieldData);
            }
            sfw.Close();
        }
Beispiel #5
0
        public static void GenerateParkingRegionShapeFromFile(string fileName, bool addDev = false)
        {
            string txtFileName = Path.Combine(Constants.DATA_DIR, "beijingTrjPart", "stat",
                                              "parkingRegion.txt");
            //string fileName = Path.Combine(Constants.DATA_DIR, "beijingTrjPart", "shp", "parkingRegion.shp");
            String rootDir       = Path.GetDirectoryName(fileName);
            String shapeFileName = Path.GetFileNameWithoutExtension(fileName);
            var    transform     = new Wgs2MgsTransform();

            // Read txt
            string[] lines = File.ReadAllLines(txtFileName);

            ShapeType shapeType = ShapeType.PolyLine;

            DbfFieldDesc[] shpFields = new DbfFieldDesc[]
            {
                new DbfFieldDesc {
                    FieldName = "ID", FieldType = DbfFieldType.Character, FieldLength = 14, RecordOffset = 0
                },
                //new DbfFieldDesc { FieldName = "Name", FieldType = DbfFieldType.Character, FieldLength = 18, RecordOffset = 14 },
            };
            ShapeFileWriter sfw = ShapeFileWriter.CreateWriter(rootDir, shapeFileName, shapeType, shpFields);

            foreach (var line in lines)
            {
                var fields = line.Split(new char[] { ',' });
                if (fields.Length % 2 != 0)
                {
                    continue;
                }
                String[]      fieldData = new string[] { " " };
                List <PointF> vertices  = new List <PointF>();
                for (int i = 0; i < fields.Length; i += 2)
                {
                    float    lat   = float.Parse(fields[i]);
                    float    lng   = float.Parse(fields[i + 1]);
                    GeoPoint point = new GeoPoint(lat, lng);
                    if (addDev)
                    {
                        point = transform.Transform(point);
                    }
                    vertices.Add(new PointF((float)point.Lng, (float)point.Lat));
                }
                sfw.AddRecord(vertices.ToArray(), vertices.Count, fieldData);
            }
            sfw.Close();
        }
Beispiel #6
0
        /// <summary>
        /// Genera la salida en SHP
        /// </summary>
        private void generaSHPSalida()
        {
            try
            {
                final = 0;
                if (items.Count > 0)
                {
                    if (String.IsNullOrEmpty(pathFile))
                    {
                        pathFile = txtCarpetaSalida.Text;
                    }

                    if (String.IsNullOrEmpty(nameFile))
                    {
                        var frmNombre = new FrmNombre();
                        frmNombre.ShowDialog();
                        nameFile = frmNombre.Texto;
                    }

                    string archivoSalidaDatos = "";
                    String nombreFichero      =
                        Path.GetFileNameWithoutExtension(String.Format("{0}\\{1}", pathFile, nameFile));
                    archivoSalidaDatos = String.Format("{0}\\{1}_salida.dat", pathFile, nombreFichero);

                    var fs = new FileStream(archivoSalidaDatos, FileMode.Create);
                    var sw = new StreamWriter(fs);

                    if (checkCovariables.Checked)
                    {
                        sw.Write("{0}{1}{2}{3}{4}{5}{6}{7}{8}", "Toponimo", separador, "Latitud", separador, "Longitud",
                                 separador, "X", separador, "Y");
                        foreach (var cov in covariables)
                        {
                            sw.Write("{0}{1}", separador, cov.Nombre);
                        }
                        sw.Write("\n");
                    }
                    else
                    {
                        sw.WriteLine("{0}{1}{2}{3}{4}{5}{6}{7}{8}", "Toponimo", separador, "Latitud", separador,
                                     "Longitud", separador, "X", separador, "Y");
                    }

                    DbfFieldDesc[]  camposdb       = null;
                    int             numDatos       = 2;
                    ShapeFileWriter shapefilewrite = null;
                    if (checkCovariables.Checked)
                    {
                        int numCovariables = covariables.Count;
                        numDatos = numCovariables + 2;
                        camposdb = new DbfFieldDesc[numCovariables + 2];
                        camposdb[0].FieldType   = DbfFieldType.Character;
                        camposdb[0].FieldLength = 50;
                        camposdb[0].FieldName   = "ObjectId";
                        camposdb[1].FieldType   = DbfFieldType.Character;
                        camposdb[1].FieldLength = 255;
                        camposdb[1].FieldName   = "Toponimo";
                        for (int i = 0; i < numCovariables; i++)
                        {
                            camposdb[i + 2].FieldName = covariables[i].Nombre;
                            switch (covariables[i].Tipo)
                            {
                            case TiposDatos.CADENA:
                                camposdb[i + 2].FieldType   = DbfFieldType.Character;
                                camposdb[i + 2].FieldLength = 255;
                                break;

                            case TiposDatos.DECIMAL:
                                camposdb[i + 2].FieldType    = DbfFieldType.FloatingPoint;
                                camposdb[i + 2].FieldLength  = 19;
                                camposdb[i + 2].DecimalCount = 8;
                                break;

                            case TiposDatos.ENTERO:
                                camposdb[i + 2].FieldType   = DbfFieldType.Number;
                                camposdb[i + 2].FieldLength = 15;
                                break;

                            case TiposDatos.FECHA:
                                camposdb[i + 2].FieldType   = DbfFieldType.Date;
                                camposdb[i + 2].FieldLength = 8;
                                break;
                            }
                        }
                    }
                    else
                    {
                        camposdb = new DbfFieldDesc[2];
                        camposdb[0].FieldType   = DbfFieldType.Character;
                        camposdb[0].FieldLength = 50;
                        camposdb[0].FieldName   = "ObjectId";
                        camposdb[1].FieldType   = DbfFieldType.Character;
                        camposdb[1].FieldLength = 255;
                        camposdb[1].FieldName   = "Toponimo";
                    }
                    shapefilewrite =
                        ShapeFileWriter.CreateWriter(pathFile, nombreFichero,
                                                     ShapeType.Point, camposdb);
                    foreach (var kv in items)
                    {
                        try
                        {
                            RegistroModel rm =
                                listadoRegistro.FirstOrDefault(obj => String.Equals(obj.Id, kv.Value.key,
                                                                                    StringComparison.InvariantCultureIgnoreCase));
                            string[] datos = new string[numDatos];
                            datos[0] = kv.Value.key;
                            datos[1] = kv.Value.Direccion;
                            sw.Write("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}", kv.Value.Direccion, separador,
                                     kv.Value.Latitud.ToString("##########.########", CultureInfo.InvariantCulture),
                                     separador,
                                     kv.Value.Longitud.ToString("##########.########", CultureInfo.InvariantCulture),
                                     separador,
                                     kv.Value.X.ToString("##########.##", CultureInfo.InvariantCulture),
                                     separador,
                                     kv.Value.Y.ToString("##########.##", CultureInfo.InvariantCulture),
                                     separador);
                            if (checkCovariables.Checked)
                            {
                                for (int i = 0; i < covariables.Count; i++)
                                {
                                    if (rm == null)
                                    {
                                        continue;
                                    }
                                    if (rm.Covariables[i] is DateTime)
                                    {
                                        var fecha = (DateTime)rm.Covariables[i];
                                        datos[i + 2] = fecha.ToString("yyyyMMdd");
                                        if (i == covariables.Count - 1)
                                        {
                                            sw.Write("{0}", fecha.ToShortDateString());
                                        }
                                        else
                                        {
                                            sw.Write("{0}{1}", fecha.ToShortDateString(), separador);
                                        }
                                    }
                                    else if (rm.Covariables[i] is Double)
                                    {
                                        var doble = (Double)rm.Covariables[i];
                                        datos[i + 2] = doble.ToString("##########.########",
                                                                      CultureInfo.InvariantCulture);
                                        if (i == covariables.Count - 1)
                                        {
                                            sw.Write("{0}",
                                                     doble.ToString("##########.########",
                                                                    CultureInfo.InvariantCulture));
                                        }
                                        else
                                        {
                                            sw.Write("{0}{1}",
                                                     doble.ToString("##########.########",
                                                                    CultureInfo.InvariantCulture),
                                                     separador);
                                        }
                                    }
                                    else
                                    {
                                        datos[i + 2] = rm.Covariables[i].ToString();
                                    }
                                }
                            }
                            sw.Write("\n");
                            if (!double.IsNaN(kv.Value.X) && !double.IsNaN(kv.Value.Y))
                            {
                                if (shapefilewrite != null)
                                {
                                    shapefilewrite.AddRecord(
                                        new[] { kv.Value.X, kv.Value.Y },
                                        1,
                                        datos);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            final = 1;
                        }
                    }//Fin foreach de items georreferenciados

                    if (shapefilewrite != null)
                    {
                        shapefilewrite.Close();
                    }
                    sw.Flush();
                    sw.Close();

                    StreamWriter spjr = new StreamWriter(String.Format("{0}\\{1}.prj", pathFile, nombreFichero));
                    spjr.Write(Settings.Default.Proyeccion);
                    spjr.Close();

                    StreamWriter scfg = new StreamWriter(String.Format("{0}\\{1}.cfg", pathFile, nombreFichero));
                    scfg.Write("UTF-8");
                    scfg.Close();
                }
                else
                {
                    MessageBox.Show(Resources.No_Existen_Registros, Resources.Error, MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                final = -1;
                BeginInvoke(new DlgMensaje(escribeConsolaRojo), new object[] { ex.Message });
            }
        }