Beispiel #1
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 });
            }
        }
Beispiel #2
0
        /// <summary>
        /// abre el archivo de texto que se va a georreferenciar
        /// </summary>
        private void abrirArchivo()
        {
            try
            {
                txtCarpetaSalida.Text = pathFile;
                string archivo = String.Format("{0}\\{1}", pathFile, nameFile);
                items.Clear();
                idRegistro      = 0;
                listadoRegistro = new List <RegistroModel>();
                var sr             = new StreamReader(archivo, true);
                int numLinea       = 0;
                int numCovariables = covariables.Count;
                //Modificaciones para SpainRDR
                while (!sr.EndOfStream)
                {
                    numLinea++;
                    var line = sr.ReadLine();
                    if (line == null)
                    {
                        continue;
                    }
                    if (checkCovariables.Checked)
                    {
                        int      lineCovariables = 0;
                        string[] campos          = line.Split(separador);
                        lineCovariables = campos.Length - 1;
                        RegistroModel registro = new RegistroModel();
                        registro.Direccion   = campos[0];
                        registro.Covariables = new object[numCovariables];
                        if (numCovariables == lineCovariables)
                        {
                            int  i         = 1;
                            bool okParseCo = true;
                            foreach (var covariable in covariables)
                            {
                                string errorParse = "";
                                try
                                {
                                    switch (covariable.Tipo)
                                    {
                                    case TiposDatos.CADENA:
                                        registro.Covariables[i - 1] = campos[i];
                                        break;

                                    case TiposDatos.DECIMAL:
                                        errorParse = "se esperaba un decimal";
                                        registro.Covariables[i - 1] = Double.Parse(campos[i],
                                                                                   CultureInfo.InvariantCulture);
                                        break;

                                    case TiposDatos.ENTERO:
                                        errorParse = "se esperaba un entero";
                                        registro.Covariables[i - 1] = Int32.Parse(campos[i]);
                                        break;

                                    case TiposDatos.FECHA:
                                        errorParse = "se esperaba una fecha";
                                        registro.Covariables[i - 1] = DateTime.Parse(campos[i],
                                                                                     CultureInfo.CurrentCulture);
                                        break;
                                    }
                                    i++;
                                }
                                catch (Exception ex)
                                {
                                    BeginInvoke(new DlgMensaje(escribeConsolaRojo),
                                                new object[]
                                    {
                                        String.Format("Error leyendo la línea {0}  {1} en la variable nº{2} : {3}",
                                                      numLinea, errorParse, i, line)
                                    });
                                    okParseCo = false;
                                }
                            }
                            if (!okParseCo)
                            {
                                continue;
                            }
                            registro.Id = idRegistro.ToString(CultureInfo.InvariantCulture);
                            listadoRegistro.Add(registro);
                            idRegistro++;
                        }
                        else
                        {
                            BeginInvoke(new DlgMensaje(escribeConsolaRojo),
                                        new object[]
                            {
                                String.Format(
                                    "Error leyendo la línea {0} se esperaban {1} variable en lugar {2}: {3}",
                                    numLinea,
                                    numCovariables, lineCovariables, line)
                            });
                        }
                    }
                    else
                    {
                        //Caso de no estar marcadas las covariables
                        //Ya está la dirección leída
                        string[]      campos   = line.Split(separador);
                        RegistroModel registro = new RegistroModel();
                        registro.Direccion = campos[0];
                        registro.Id        = idRegistro.ToString();
                        listadoRegistro.Add(registro);
                        idRegistro++;
                    }
                }
                IGeocodificador geodificadorInstancia =
                    GeocodingFactory.GetGeodificadorInstancia(
                        (GeoToMap.ImplementacionGeocodificador)cboGeocodificador.SelectedValue);
                var dlgProcesoGeoRreferenciacion = new DlgVoid(
                    () => ProcesoLoteGeoreferenciacion
                    (
                        geodificadorInstancia,
                        listadoRegistro
                    )
                    );
                dlgProcesoGeoRreferenciacion.BeginInvoke(finProcesoLoteGeoreferenciacion, null);
            }
            catch (Exception ex)
            {
                BeginInvoke(new DlgMensaje(escribeConsolaRojo),
                            new object[]
                {
                    ex.Message
                });
            }
        }