points.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <grass/imagery.h>
  2. #define POINT_FILE "POINTS"
  3. static int I_read_control_points (
  4. FILE *fd,
  5. struct Control_Points *cp)
  6. {
  7. char buf[100];
  8. double e1,e2,n1,n2;
  9. int status;
  10. cp->count = 0;
  11. /* read the control point lines. format is:
  12. image_east image_north target_east target_north status
  13. */
  14. cp->e1 = NULL;
  15. cp->e2 = NULL;
  16. cp->n1 = NULL;
  17. cp->n2 = NULL;
  18. cp->status = NULL;
  19. while (G_getl (buf, sizeof buf, fd))
  20. {
  21. G_strip(buf);
  22. if (*buf == '#' || *buf == 0) continue;
  23. if (sscanf (buf, "%lf%lf%lf%lf%d", &e1, &n1, &e2, &n2, &status) == 5)
  24. I_new_control_point (cp, e1, n1, e2, n2, status);
  25. else
  26. return -4;
  27. }
  28. return 1;
  29. }
  30. /*!
  31. * \brief add new control point
  32. *
  33. * Once the
  34. * control points have been read into the <b>cp</b> structure, this routine
  35. * adds new points to it. The new control point is given by <b>e1</b> (column)
  36. * and <b>n1</b> (row) on the image, and the <b>e2</b> (east) and <b>n2</b>
  37. * (north) for the target database. The value of <b>status</b> should be 1 if
  38. * the point is a valid point; 0 otherwise.\remarks{Use of this routine implies
  39. * that the point is probably good, so <b>status</b> should be set to 1.}
  40. *
  41. * \param cp
  42. * \param e1
  43. * \param n1
  44. * \param e2
  45. * \param n2
  46. * \param status
  47. * \return int
  48. */
  49. int I_new_control_point (struct Control_Points *cp,
  50. double e1,double n1,double e2,double n2, int status)
  51. {
  52. int i;
  53. unsigned int size;
  54. if (status < 0) return 1;
  55. i = (cp->count)++ ;
  56. size = cp->count * sizeof(double) ;
  57. cp->e1 = (double *) G_realloc (cp->e1, size);
  58. cp->e2 = (double *) G_realloc (cp->e2, size);
  59. cp->n1 = (double *) G_realloc (cp->n1, size);
  60. cp->n2 = (double *) G_realloc (cp->n2, size);
  61. size = cp->count * sizeof(int) ;
  62. cp->status = (int *) G_realloc (cp->status, size);
  63. cp->e1[i] = e1;
  64. cp->e2[i] = e2;
  65. cp->n1[i] = n1;
  66. cp->n2[i] = n2;
  67. cp->status[i] = status;
  68. return 0;
  69. }
  70. static int I_write_control_points(FILE *fd, const struct Control_Points *cp)
  71. {
  72. int i;
  73. fprintf (fd,"# %7s %15s %15s %15s %9s status\n","","image","","target","");
  74. fprintf (fd,"# %15s %15s %15s %15s (1=ok)\n","east","north","east","north");
  75. fprintf (fd,"#\n");
  76. for (i = 0; i < cp->count; i++)
  77. if (cp->status[i] >= 0)
  78. fprintf (fd, " %15f %15f %15f %15f %4d\n",
  79. cp->e1[i], cp->n1[i], cp->e2[i], cp->n2[i], cp->status[i]);
  80. return 0;
  81. }
  82. /*!
  83. * \brief read group control points
  84. *
  85. * Reads the control points from the POINTS file
  86. * for the <b>group</b> into the <b>cp</b> structure. Returns 1 if
  87. * successful; 0 otherwise (and prints a diagnostic error).
  88. * <b>Note.</b> An error message is printed if the POINTS file is invalid, or
  89. * does not exist.
  90. *
  91. * \param group
  92. * \param cp
  93. * \return int
  94. */
  95. int I_get_control_points (
  96. const char *group,
  97. struct Control_Points *cp)
  98. {
  99. FILE *fd;
  100. char msg[100];
  101. int stat;
  102. fd = I_fopen_group_file_old (group, POINT_FILE);
  103. if (fd == NULL)
  104. {
  105. sprintf (msg, "unable to open control point file for group [%s in %s]",
  106. group, G_mapset());
  107. G_warning (msg);
  108. return 0;
  109. }
  110. stat = I_read_control_points (fd, cp);
  111. fclose (fd);
  112. if (stat < 0)
  113. {
  114. sprintf (msg, "bad format in control point file for group [%s in %s]",
  115. group, G_mapset());
  116. G_warning (msg);
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. /*!
  122. * \brief write group control points
  123. *
  124. * Writes the control points from the
  125. * <b>cp</b> structure to the POINTS file for the specified group.
  126. * <b>Note.</b> Points in <b>cp</b> with a negative <i>status</i> are not
  127. * written to the POINTS file.
  128. *
  129. * \param group
  130. * \param cp
  131. * \return int
  132. */
  133. int I_put_control_points (
  134. const char *group,
  135. const struct Control_Points *cp)
  136. {
  137. FILE *fd;
  138. char msg[100];
  139. fd = I_fopen_group_file_new (group, POINT_FILE);
  140. if (fd == NULL)
  141. {
  142. sprintf (msg, "unable to create control point file for group [%s in %s]",
  143. group, G_mapset());
  144. G_warning (msg);
  145. return 0;
  146. }
  147. I_write_control_points (fd, cp);
  148. fclose (fd);
  149. return 1;
  150. }