points.c 3.9 KB

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