Hi:

I do think it could be interesting, in fact I am using a couple of modules that performs Horton and Strahler ordering with my students. I just regret not be able to be more participative in this list.

I attach you the code in case it could be helpful.

The code it's a bit of a mess (not to mention that the comments are in Spanish ;-)

It assumes that the tables linked to the vector data are in postgresql.

There are two modules:

v.ordena_red (network ordering), performs Strahler or Shreve ordering (crit parameter). "desembocadura" means outlet (you have to pass the cat value of the oulet reach).

v.str2hor converts an ordering from Horton to Strahler.

They work fine as long as your network nodes have only 3 links.

In case have any problem, you should delete all the created files and tables to rerun them.

Good luck.

Paco

Jarek Jasiewicz <jar...@amu.edu.pl> escribió:

Hi list!

I've just thinked how to create horton stream order based on my module strahler module and accumulation file. Hortons stream order differs sligty form strahler order, see for example here fig. 1 page 78 http://iahs.info/hsj/134/134008.pdf

Because I have some tests results from some of you (many thanks for your courage) i'm about to finish module, modify code to meet grass requiments and officialy anouce it on grass-dev, but If I will add horton stream order it will may take a bit more time.

So I have 2 question  about that (horton):

Did you think taht horton stream order has already sense (it is almost not in use)

Does any body know any program (commercial or not) which calculate horton's stream order

Thanks for ansvers
Jarek
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


#include "grass/gis.h"
#include "grass/Vect.h"
#include "grass/display.h"
#include "grass/raster.h"
#include "grass/colors.h"
#include <grass/dbmi.h>
#include <grass/codes.h>
#include "libreria.h"
#include "math.h"

int crea_db(char* nombre_tabla,int *ordenacion,struct Map_info *map);

int main (int argc,char *argv[]){

  struct GModule *module;         //Definición del módulo
  struct Map_info map,map2;       //Mapas
  struct Option *input, *output,*crit,*des;  //Opciones de usuario
  struct line_pnts *linea,*linea1,*linea2;  //Secuencias de puntos
  struct line_cats *cats;


  int lev,n1,n2,criterio,desemb,busc;
  int i,ii,l,k,l1,l2,l3,l4,ll,n_lines,n_nodes,fin,p,prev;
  char *mapset, err_msg[200],buf[1024],*name,*name2;
  double *recta,*sinuosidad,*longit,long2,recta2,sinu2;
  int *orden,*sigue1,*sigue2,*tocado,*coming,*desem,*att,*n_lineas,*fuente1,*fuente2,*fuente3;
  double x,y;
  char *colorin;


  //Variables de la base de datos
  char *db_nombre, *driver_nombre, *ordenc;

  dbDriver *driver;
  dbHandle handle;
  dbTable *tabla;
  dbColumn *columna;

  dbString orden_sql;

  ordenc=(char *) malloc(200);
  colorin=(char *) malloc(11);

  G_gisinit (argv[0]);


  // Gestión de la linea de comandos

  module = G_define_module();
  module->description ="Ordena una red de drenaje generada artificialmente";

  input = G_define_option();
  input->key = "input";
  input->type = TYPE_STRING;
  input->required = YES;
  input->description = "Nombre del mapa vectorial que vas a abrir";
  input->gisprompt = "viejo,vectorial,mapa vectorial";

  output = G_define_option();
  output->key = "output";
  output->type = TYPE_STRING;
  output->required = YES;
  output->description = "Nombre del mapa vectorial que vas a crear";
  output->gisprompt = "nuevo,vectorial,mapa vectorial";

  crit = G_define_option();
  crit->key = "crit";
  crit->type = TYPE_INTEGER;
  crit->required = YES;
  crit->description = "Criterio de ordenación (1:Strahler 2:Shreve 3:Crea tabla)";
  crit->gisprompt = "";

  des = G_define_option();
  des->key = "desembocadura";
  des->type = TYPE_INTEGER;
  des->required = YES;
  des->description = "cat del tramo de desembocadura de la red";
  des->gisprompt = "";

  if (G_parser(argc,argv))  exit(1);

  criterio=atoi(crit->answer);
  desemb=atoi(des->answer);


  //Inicialización de comandos gráficos
  if (R_open_driver()!=0) G_fatal_error("No hay monitor\n");
  D_setup(0);//R_standard_color(0);
  G_setup_plot(D_get_d_north(),D_get_d_south(),D_get_d_west(),D_get_d_east(),D_move_abs,D_cont_abs);



  //COMENZAR LA CREACION DE LA BASE DE DATOS

  //Establecer parámetros de la conexión  
  driver_nombre=db_get_default_driver_name();
  db_nombre=db_get_default_database_name();
 

  //Abrimos
  driver=db_start_driver(driver_nombre);
  db_init_handle(&handle);
  db_set_handle(&handle,db_nombre,NULL);
  if(db_open_database(driver,&handle)!=DB_OK){exit(ERROR);}


  //Generar y comprobar la orden SQL de creación de tabla
  db_init_string(&orden_sql);
  db_zero_string (&orden_sql);
  sprintf(ordenc,"create table %s ( cat integer, ",output->answer);
  db_append_string(&orden_sql,ordenc);  
  db_append_string(&orden_sql,"orden integer, longitud double precision, GRASSRGB varchar(11) );");
  ordenc=db_get_string(&orden_sql);
  printf("%s\n",ordenc);

  
  //Ejecutar (crear la tabla, hacer un índice y dar permisos)
  printf("conexión: %s %s\n",driver_nombre,db_nombre);
  printf("Ejecuta: %d\n",db_execute_immediate (driver, &orden_sql)); 
  printf("Indice: %d\n",db_create_index2(driver, output->answer, "cat" ));
  printf("GRANT: %d\n",db_grant_on_table (driver, output->answer, DB_PRIV_SELECT, DB_GROUP|DB_PUBLIC ));

  //db_commit_transaction(driver);db_close_database(driver);

  //db_close_database(driver);
  // db_shutdown_driver(driver);

  // Abre el mapa vectorial de entrada

  name = input->answer;
  mapset = G_find_file ("vector", name, "");
  if (mapset == NULL) {
    sprintf (err_msg, "Could not find vector file [%s]", name);
    G_fatal_error (err_msg);
    }
    
  printf("HOLA\n");
  lev=Vect_open_old (&map, name, mapset);
  if (0 > lev) {
    sprintf (err_msg, "Could not open vector file [%s]\n", name);
    G_fatal_error (err_msg);
    }

    n_lines=Vect_get_num_lines(&map);
    n_nodes=Vect_get_num_nodes(&map);

    printf("Mapa %s abierto en  nivel %d.\n",name,lev);    
    printf("%d areas, %d lineas, %d nodos %d islas %d atts\n",Vect_get_num_areas(&map),n_lines,n_nodes,Vect_get_num_islands(&map));

    if (criterio!=3){  //Abre el mapa vectorial de salida
      name2 = output->answer;
      lev=Vect_open_new (&map2, name2,0);
      if (0 > lev) {
                 sprintf (err_msg, "Could not open vector file [%s]\n", name2);
                 G_fatal_error (err_msg);
      }
      printf("Mapa %s abierto en  nivel %d.\n",name2,lev);
  }

  printf("HOLA\n");
  //Inicialización de estructuras y arrays
  cats=Vect_new_cats_struct();
  linea=Vect_new_line_struct();
  linea1=Vect_new_line_struct();
  linea2=Vect_new_line_struct();

  if (!(tocado=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 1\n");
  if (!(orden=G_calloc(n_nodes+1,sizeof(int)))) printf("ERROR 2\n");
  if (!(coming=G_calloc(n_nodes+1,sizeof(int)))) printf("ERROR 3\n");
  if (!(longit=G_calloc(n_lines+1,sizeof(double)))) printf("ERROR 4\n");
  if (!(sigue1=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 5\n");
  if (!(sigue2=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 6\n");
  if (!(recta=G_calloc(n_lines+1,sizeof(double)))) printf("ERROR 7\n");
  if (!(sinuosidad=G_calloc(n_lines+1,sizeof(double)))) printf("ERROR 8\n");

  if (!(n_lineas=G_calloc(n_nodes+1,sizeof(int)))) printf("ERROR 9\n");
  if (!(fuente1=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 10\n");
  if (!(fuente2=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 11\n");
  if (!(fuente3=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 12\n");
  if (!(desem=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 13\n");
  if (!(att=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 14\n");

  for (l=1;l<=n_lines;l++){
      Vect_read_line(&map,linea,cats,l);
      longit[l]=longitud(linea);
      recta[l]=lonrec(linea);
      sinuosidad[l]=longit[l]/recta[l];
      orden[l]=0;
      sigue1[l]=-99;sigue2[l]=-99;
      // printf("linea %d, %f m\n",l,longit[l]);
  }

  //Busqueda de lineas con orden 1
  for (i=1;i<=n_nodes;i++){
       n_lineas[i]=(map.plus.Node[i])->n_lines;
       printf("%d: %d",i,n_lineas[i]);
       if (n_lineas[i]==1){
          l=abs(*((map.plus.Node[i])->lines));
          printf(" %d ",l);if (l==desemb){printf("desembocadura\n");}
          else {
             orden[l]=1;printf("%d orden 1\n",l);
             Vect_read_line(&map,linea,cats,l); pintalinea(linea,orden[l]); R_stabilize();
          }
       } 
       else{printf("\n");}
  }
  busc=1;
  if (busc==1){
 
  //printf("BUSQUEDA DE ORDEN >1\n");
  fin=-1;
  while (fin!=0){
      fin=0;      
      for (i=1;i<=n_nodes;i++){      //Para todos los nodos

         if (n_lineas[i]==3){                     //Si es un nodo con 3 lineas (caso normal)
            k=0;
            l1=abs(*((map.plus.Node[i])->lines));
            l2=abs(*((map.plus.Node[i])->lines+1));
            l3=abs(*((map.plus.Node[i])->lines+2));
            if (orden[l1]!=0) k++;if (orden[l2]!=0) k++;if (orden[l3]!=0) k++;
	    if (k==2){
	      ll=ordena(l1,&(orden[l1]),l2,&(orden[l2]),l3,&(orden[l3]),criterio);fin++;
               prev=0;
               if (orden[ll]==orden[l1] && l1!=ll) prev=l1;
               if (orden[ll]==orden[l2] && l2!=ll) prev=l2;
               if (orden[ll]==orden[l3] && l3!=ll) prev=l3;
               if (ll==l1){fuente1[ll]=l2;fuente2[ll]=l3;desem[l2]=ll;desem[l3]=ll;}
               if (ll==l2){fuente1[ll]=l1;fuente2[ll]=l3;desem[l1]=ll;desem[l3]=ll;}
               if (ll==l3){fuente1[ll]=l1;fuente2[ll]=l2;desem[l1]=ll;desem[l2]=ll;}
               if (prev!=0){
                  if (sigue1[prev]==-99) sigue1[prev]=ll; else sigue2[prev]=ll;
                  if (sigue1[ll]==-99) sigue1[ll]=prev; else sigue2[ll]=prev;
	       }
	       //printf("el nodo %d tiene %d lineas\n",i,n_lineas[i]);
	       //printf("%d %d   %d %d   %d %d \n",l1,orden[l1],l2,orden[l2],l3,orden[l3]);
Vect_read_line(&map,linea,cats,ll); pintalinea(linea,orden[ll]);
               R_stabilize();
	    }   
	 }

      }
  }
  }
  printf("\nEscritura de los ficheros\n");
  //if(db_open_database(driver,&handle)!=DB_OK){exit(ERROR);}
  for (i=1;i<=n_lines;i++){
 
     if (criterio!=3){
 
        if (sigue1[i]!=-99 && sigue2[i]==-99 && tocado[i]==0){
           
	  printf("Procesando linea %d\n",i);
           p=sigue1[i];linea1=Vect_new_line_struct(); 
           Vect_read_line(&map,linea1,cats,i);n1=linea1->n_points;
           fin=0;ii=i;

           while(fin==0){  
              tocado[p]=1;tocado[ii]=1;
              linea2=Vect_new_line_struct();Vect_read_line(&map,linea2,cats,p);
              n1=linea1->n_points;n2=linea2->n_points;

              printf("l1=%d n1=%d    l2=%d n2=%d\n",i,n1,p,n2);

	      linea=Vect_new_line_struct();une_tramos(linea1,linea2,linea);

              if (sigue1[p]==ii && sigue2[p]==-99){
                   fin=1;
                   Vect_write_line(&map2,2,linea,cats);
                   long2=longitud(linea);recta2=lonrec(linea);sinu2=long2/recta2;
                   //Añadir linea en la base de datos
                   if(orden[p]==1) strcpy(colorin,"0:0:255");
                   if(orden[p]==2) strcpy(colorin,"255:255:0");
                   if(orden[p]==3) strcpy(colorin,"0:255:0");
                   if(orden[p]==4) strcpy(colorin,"255:0:0");
                   if(orden[p]==5) strcpy(colorin,"0:255:255");
                   if(orden[p]==6) strcpy(colorin,"255:0:255");
                   if(orden[p]>6) strcpy(colorin,"0:0:0");
                   sprintf(ordenc,"insert into %s values(%d,%d,%f,'%s');",name2,p,orden[p],long2,colorin); 
                   printf("%s\n",ordenc);
                   db_set_string(&orden_sql,ordenc); 
                   printf("Ejecuta: %d\n",db_execute_immediate(driver, &orden_sql)); 
                   Vect_destroy_line_struct(linea);
                   Vect_destroy_line_struct(linea1);
                   Vect_destroy_line_struct(linea2);
              }  
              else{
    	          if (sigue1[p]==ii){ii=p;p=sigue2[p];}
	          if (sigue2[p]==ii){ii=p;p=sigue1[p];}
		  copia_tramos(linea,linea1);
                  Vect_destroy_line_struct(linea);
              }             
           
	   }
	}
  
        if (sigue1[i]==-99 && sigue2[i]==-99){
            linea=Vect_new_line_struct();
             Vect_read_line(&map,linea,cats,i);
             printf("linea %d tal cual\n",i);
             Vect_write_line(&map2,2,linea,cats);
                   if(orden[i]==1) strcpy(colorin,"0:0:255");
                   if(orden[i]==2) strcpy(colorin,"255:255:0");
                   if(orden[i]==3) strcpy(colorin,"0:255:0");
                   if(orden[i]==4) strcpy(colorin,"255:0:0");
                   if(orden[i]==5) strcpy(colorin,"0:255:255");
                   if(orden[i]==6) strcpy(colorin,"255:0:255");
                   if(orden[i]>6) strcpy(colorin,"0:0:0");

             sprintf(ordenc,"insert into %s values(%d,%d,%f,'%s');",name2,i,orden[i],longit[i],colorin);
             printf("%s\n",ordenc);
             db_set_string(&orden_sql,ordenc); 
             printf("Ejecuta: %d\n",db_execute_immediate(driver, &orden_sql)); 
             Vect_destroy_line_struct(linea);
        }
     }
  }

  //Enlace mapa-tabla
  Vect_map_add_dblink(&map2,1,NULL,name2,"cat",db_nombre,driver_nombre);
  Vect_build(&map2, stdout);

  //Estamos cerrando
  R_close_driver();
  Vect_close(&map);
  Vect_close(&map2);

  if (driver){
    //db_commit_transaction(driver); //Creo que garantiza que no queden operaciones a medio
    db_close_database(driver);
    db_shutdown_driver(driver);
  }
}
#include "grass/gis.h"
#include "grass/Vect.h"
#include "grass/display.h"
#include "grass/raster.h"
#include "grass/colors.h"
#include <grass/dbmi.h>
#include <grass/codes.h>
#include "libreria.h"

int main(int argc,char *argv[]){
  struct GModule *module;         //Definición del módulo
  struct Map_info map,map2;       //Mapas
  struct Option *input, *output;  //Opciones de usuario

  struct line_pnts *linea,*linea1,*linea2;  //Secuencias de puntos;
  struct line_cats *cats,*refcats;

  dbDriver *driver;
  dbHandle handle;
  dbTable *tabla;
  dbColumn *columna;
  dbValue *valor;
  dbCursor cursor;

  dbString orden_sql,valores;

  char *db_nombre, *driver_nombre,*name,*name2,*mapset,err_msg[200],*ordenc;
  int i,j,lev,n_lines,n_nodes,n_rows,p,pp;
  int ltype=0;
  int col,ncols,more;
  int *mapacat,dbcat,*dborden;
  double *dblong;
 
  int *tocado,*norden,no_orden,*previo,*sigue2,maxo,u,k,h,eti,n1,n2,l11,l12,l21,l22,l13,l23,divo,*signo;
  int *n_lineas,prev[2];
  double *recta,*sinuosidad,*longit,lonref;
  char *colorin;

  ordenc=(char *) malloc(200);
  colorin=(char *) malloc(11);

  G_gisinit (argv[0]);


  // Gestión de la linea de comandos

  module = G_define_module();
  module->description ="Reordena una red de drenaje de Strhaler a Horton";

  input = G_define_option();
  input->key = "input";
  input->type = TYPE_STRING;
  input->required = YES;
  input->description = "Nombre del mapa vectorial que vas a abrir";
  input->gisprompt = "viejo,vectorial,mapa vectorial";

  output = G_define_option();
  output->key = "output";
  output->type = TYPE_STRING;
  output->required = YES;
  output->description = "Nombre del mapa vectorial que vas a crear";
  output->gisprompt = "nuevo,vectorial,mapa vectorial";

  if (G_parser(argc,argv))  exit(1);

  linea=Vect_new_line_struct();
  cats=Vect_new_cats_struct();

  // Abre el mapa vectorial de entrada

  name = input->answer;
  mapset = G_find_file ("vector", name, "");
  if (mapset == NULL) {
    sprintf (err_msg, "Could not find vector file [%s]", name);
    G_fatal_error (err_msg);
    }
  lev=Vect_open_old (&map, name, mapset);
  if (0 > lev) {
    sprintf (err_msg, "Could not open vector file [%s]\n", name);
    G_fatal_error (err_msg);
  }

  n_lines=Vect_get_num_lines(&map);
  n_nodes=Vect_get_num_nodes(&map);

  printf("Mapa %s abierto en  nivel %d.\n",name,lev);   
  printf("%d areas, %d lineas, %d nodos %d islas %d atts\n",Vect_get_num_areas(&map),n_lines,n_nodes,Vect_get_num_islands(&map));


  //Lee categorías
  mapacat=(int *) G_calloc(n_lines+1,sizeof(int)); 
  dborden=(int *) G_calloc(n_lines+2,sizeof(int));
  dblong=(double *) G_calloc(n_lines+1,sizeof(double));
  for (i=1;i<=n_lines;i++){
      ltype=Vect_read_line(&map,linea,cats,i);
      if(ltype>=0){*(mapacat+i)=cats->cat[0];printf("%d %d\n",i,*(mapacat+i));}  
  }
  
  // Reserva de memoria
  signo=G_calloc(n_lines+1,sizeof(int));  
  tocado=G_calloc(n_lines+1,sizeof(int));
  norden=G_calloc(n_lines+1,sizeof(int));
  previo=G_calloc(n_lines+1,sizeof(int));
  sigue2=G_calloc(n_lines,sizeof(int));
  longit=G_calloc(n_lines+1,sizeof(double));
  sinuosidad=G_calloc(n_lines,sizeof(double));
  n_lineas=G_calloc(n_nodes,sizeof(int));

  printf("HOLA\n");
  linea=Vect_new_line_struct();
  linea1=Vect_new_line_struct();
  linea2=Vect_new_line_struct();

  printf("HOLA\n");

  //TRABAJO CON LA BASE DE DATOS

  //Establecer parámetros de la conexión  
  driver_nombre=db_get_default_driver_name();
  db_nombre=db_get_default_database_name();
  driver=db_start_driver_open_database(driver_nombre,db_nombre);

  //Abrimos
  driver=db_start_driver(driver_nombre);
  db_init_handle(&handle);
  db_set_handle(&handle,db_nombre,NULL);
  if(db_open_database(driver,&handle)!=DB_OK){exit(ERROR);}


  //Generar, comprobar y ejecutar la orden SQL de consulta
  db_init_string(&orden_sql);  db_zero_string (&orden_sql);
  db_init_string(&valores);
  sprintf(ordenc,"select cat,orden,longitud from %s;",input->answer);
  db_append_string(&orden_sql,ordenc);  
  ordenc=db_get_string(&orden_sql);
  printf("%s\n",ordenc);
  printf("conexión: %s %s\n",driver_nombre,db_nombre);
  if(db_open_select_cursor(driver,&orden_sql,&cursor,DB_SEQUENTIAL)!=DB_OK) return ERROR; 

  //Procesar resultados

  tabla=db_get_cursor_table(&cursor);
  ncols=db_get_table_number_of_columns(tabla);

  while(1){
    if(db_fetch(&cursor,DB_NEXT,&more)!=DB_OK) return ERROR;
    if(!more) break;
 
    columna=db_get_table_column(tabla,0);
    db_convert_column_value_to_string(columna,&valores);
    dbcat=atoi(db_get_string(&valores)); 
    for (i=1;i<=n_lines;i++){  
        if(dbcat==mapacat[i]){
           columna=db_get_table_column(tabla,1);
           db_convert_column_value_to_string(columna,&valores);
           *(dborden+i)=atoi(db_get_string(&valores));
           columna=db_get_table_column(tabla,2);
           db_convert_column_value_to_string(columna,&valores);
           *(dblong+i)=atof(db_get_string(&valores));
           printf("%d %d %d %f\n",i,*(mapacat+i),*(dborden+i),*(dblong+i));
           if(*(dborden+i)>maxo) maxo=*(dborden+i);
        }
    }    
  };n_rows=i;printf("%d filas max(o)=%d\n",n_rows,maxo);

  //Cerramos
  db_close_database(driver);
  db_shutdown_driver(driver);


  //COMIENZA LA TOPOLOGIA


  // Calcular la topología
  for (u=2;u<=maxo;u++){
      for (i=1;i<=n_lines;i++){
	  if (dborden[i]==u){
	      previo[i]=0;prev[0]=0;prev[1]=0;
	      eti=*(mapacat+i);
              h=0;lonref=0;
              n1=(map.plus.Line[i])->N1; 
              n2=(map.plus.Line[i])->N2;
              printf("i=%d cat=%d o=%d  n1=%d n2=%d\n",i,eti,dborden[i],n1,n2);

              for(k=0;k<(map.plus.Node[n1])->n_lines;k++){
                  l11=abs(*(((map.plus.Node[n1])->lines)+k));
                  if(dborden[l11]==u-1 && dblong[l11]>lonref){
	               prev[0]=l11;lonref=dblong[l11];
		  }
                  if(dborden[l11]>u){lonref=0;prev[0]=0;break;} 
              }

              for(k=0;k<(map.plus.Node[n2])->n_lines;k++){
                  l21=abs(*(((map.plus.Node[n2])->lines)+k));
                  if(dborden[l21]==u-1 && dblong[l21]>lonref){
                       prev[1]=l21;lonref=dblong[l21];
                  } 
                  if(norden[l21]>u){lonref=0;prev[1]=0;break;} 
	      }

              if (prev[0]==0 && prev[1]!=0)previo[i]=prev[1];
              if (prev[1]==0 && prev[0]!=0)previo[i]=prev[0];
              if (prev[1]!=0 && prev[0]!=0){
                  if(dblong[prev[0]]>dblong[prev[1]]) previo[i]=prev[0]; 
                  else previo[i]=prev[1];
	      }
              *(dblong+i)=*(dblong+i)+*(dblong+previo[i]);
	      printf("long=%f  o=%d  pre=%d  cat=%d\n\n",*(dblong+i),dborden[i],previo[i],mapacat[previo[i]]);
          }
      }
  }
    

  // Preparar el fichero de salida
  name2 = output->answer; printf("hola\n");
  lev=Vect_open_new (&map2, name2,0); printf("hola\n");
  if (0 > lev) {sprintf (err_msg, "No se abre [%s]\n", name2); G_fatal_error (err_msg);}
  printf("Mapa %s abierto en  nivel %d.\n",name2,lev);


  //COMENZAR LA CREACION DE LA NUEVA BASE DE DATOS

  //Establecer parámetros de la conexión  
  driver_nombre=db_get_default_driver_name();
  db_nombre=db_get_default_database_name();
 

  //Abrimos
  driver=db_start_driver(driver_nombre);
  db_init_handle(&handle);
  db_set_handle(&handle,db_nombre,NULL);
  if(db_open_database(driver,&handle)!=DB_OK){exit(ERROR);}

  //Generar y comprobar la orden SQL de creación de tabla
  db_init_string(&orden_sql);
  db_zero_string (&orden_sql);
  sprintf(ordenc,"create table %s ( cat integer, ",output->answer);
  db_append_string(&orden_sql,ordenc);  
  db_append_string(&orden_sql,"orden integer, longitud double precision, GRASSRGB varchar(11) );");
  ordenc=db_get_string(&orden_sql);
  printf("%s\n",ordenc);

  
  //Ejecutar (crear la tabla, hacer un índice y dar permisos)
  printf("conexión: %s %s\n",driver_nombre,db_nombre);
  printf("Ejecuta: %d\n",db_execute_immediate (driver, &orden_sql)); 
  printf("Indice: %d\n",db_create_index2(driver, output->answer, "cat" ));
  printf("GRANT: %d\n",db_grant_on_table (driver, output->answer, DB_PRIV_SELECT
, DB_GROUP|DB_PUBLIC ));



  for (u=maxo;u>=2;u--){
      for (i=1;i<=n_lines;i++){
	printf("test %d %d\n",i,dborden[i]);
	  if (dborden[i]==u && tocado[i]==0){
             
	     p=i;pp=previo[p];
             linea1=Vect_new_line_struct(); Vect_read_line(&map,linea1,cats,p);
             printf("o=%d l=%d cat=%d %d \n",u,i,mapacat[i],cats->cat[0]);
  	     while(pp!=0){
	        tocado[p]=1;
	        printf("o=%d l=%d cat=%d l2=%d cat2=%d o2=%d\n",u,p,mapacat[p],pp,mapacat[pp],dborden[pp]);
                linea2=Vect_new_line_struct();Vect_read_line(&map,linea2,cats,pp);

	        linea=Vect_new_line_struct();une_tramos(linea1,linea2,linea);
		copia_tramos(linea,linea1);
                Vect_destroy_line_struct(linea);
                p=pp;pp=previo[p];tocado[p]=1;
             }  
	     printf("hola1 %d  %d  %d\n",i,dborden[i],mapacat[i]);
             refcats=Vect_new_cats_struct();
             Vect_cat_set(refcats,1,*(mapacat+i));
             Vect_write_line(&map2,2,linea1,refcats);
             Vect_destroy_cats_struct(refcats);
	     printf("hola2 %d  %d  %d\n",i,dborden[i],mapacat[i]);
	     //Añadir la correspondiente linea a la base de datos
	        if(dborden[i]==1) strcpy(colorin,"0:0:255");
	        if(dborden[i]==2) strcpy(colorin,"255:255:0");
                if(dborden[i]==3) strcpy(colorin,"0:255:0");
                if(dborden[i]==4) strcpy(colorin,"255:0:0");
                if(dborden[i]==5) strcpy(colorin,"0:255:255");
                if(dborden[i]==6) strcpy(colorin,"255:0:255");
                if(dborden[i]>6) strcpy(colorin,"0:0:0");
                 
                printf("hola3 %d %d %d \n",i,mapacat[i],dborden[i]);
                sprintf(ordenc,"insert into %s values(%d,%d,%f,'%s');",name2,mapacat[i],dborden[i],dblong[i],colorin); 
                printf("%s\n",ordenc);
                db_set_string(&orden_sql,ordenc); 
                printf("Ejecuta: %d\n",db_execute_immediate(driver, &orden_sql)); 


             Vect_destroy_line_struct(linea1);
          }
      }
  }
          

  for (i=1;i<=n_lines;i++){
      if (tocado[i]==0){
             linea=Vect_new_line_struct();
             Vect_read_line(&map,linea,cats,i);
             refcats=Vect_new_cats_struct();printf("hola\n");
             Vect_cat_set(refcats,1,*(mapacat+i));printf("hola\n");
             Vect_write_line(&map2,2,linea,refcats);
	     Vect_destroy_cats_struct(refcats); 
	     //Añadir la correspondiente linea a la base de datos
	        if(dborden[i]==1) strcpy(colorin,"0:0:255");
              if(dborden[i]==2) strcpy(colorin,"255:255:0");
              if(dborden[i]==3) strcpy(colorin,"0:255:0");
	       if(dborden[i]==4) strcpy(colorin,"255:0:0");
	       if(dborden[i]==5) strcpy(colorin,"0:255:255");
	       if(dborden[i]==6) strcpy(colorin,"255:0:255");
	      if(dborden[i]>6) strcpy(colorin,"0:0:0");

	     sprintf(ordenc,"insert into %s values(%d,%d,%f,'%s');",name2,mapacat[i],dborden[i],dblong[i],colorin); 
	         printf("%s\n",ordenc);
		      db_set_string(&orden_sql,ordenc); 
		         printf("Ejecuta: %d\n",db_execute_immediate(driver, &orden_sql)); 
             Vect_destroy_line_struct(linea);
      }
  }
  //Enlace mapa-tabla. build y copia de cabeceras

  Vect_copy_head_data(&map,&map2);
  Vect_map_add_dblink(&map2,1,NULL,name2,"cat",db_nombre,driver_nombre);
  Vect_build(&map2, stdout);

  Vect_close(&map);
  Vect_close(&map2);



}
   
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Reply via email to