~ubuntu-branches/ubuntu/wily/spatialite/wily-proposed

« back to all changes in this revision

Viewing changes to src/shapefiles/validator.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-07-14 11:57:46 UTC
  • mfrom: (16.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150714115746-e2iljfmb5sq7o5hh
Tags: 4.3.0-1
Move from experimental to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
 validator.c -- implements geometry validation and repair
4
4
 
5
 
 version 4.1, 2013 March 19
 
5
 version 4.3, 2015 June 29
6
6
 
7
7
 Author: Sandro Furieri a.furieri@lqt.it
8
8
 
24
24
 
25
25
The Initial Developer of the Original Code is Alessandro Furieri
26
26
 
27
 
Portions created by the Initial Developer are Copyright (C) 2008-2013
 
27
Portions created by the Initial Developer are Copyright (C) 2008-2015
28
28
the Initial Developer. All Rights Reserved.
29
29
 
30
30
Alternatively, the contents of this file may be used under the terms of
1667
1667
    return;
1668
1668
}
1669
1669
 
1670
 
SPATIALITE_DECLARE int
1671
 
sanitize_geometry_column (sqlite3 * sqlite, const char *table, const char *geom,
1672
 
                          const char *tmp_table, const char *report_path,
1673
 
                          int *n_invalids, int *n_repaired, int *n_discarded,
1674
 
                          int *n_failures, char **err_msg)
 
1670
static int
 
1671
sanitize_geometry_column_common (const void *p_cache, sqlite3 * sqlite,
 
1672
                                 const char *table, const char *geom,
 
1673
                                 const char *tmp_table, const char *report_path,
 
1674
                                 int *n_invalids, int *n_repaired,
 
1675
                                 int *n_discarded, int *n_failures,
 
1676
                                 char **err_msg)
1675
1677
{
 
1678
#ifdef ENABLE_LWGEOM            /* only if LWGEOM is enabled */
 
1679
 
1676
1680
/* attempts to repair invalid Geometries from a Geometry Column */
1677
1681
    char *sql;
1678
1682
    char *xtable;
1704
1708
 
1705
1709
    if (!check_table_column (sqlite, table, geom, &gtype, &srid))
1706
1710
      {
1707
 
          spatialite_e ("sanitize_geometry_column error: <%s>\n"
 
1711
          spatialite_e ("sanitize_geometry_column error: <%s><%s>\n"
1708
1712
                        "Not defined in \"geometry_columns\"", table, geom);
1709
1713
          if (err_msg != NULL)
1710
1714
            {
1977
1981
                if (geom)
1978
1982
                  {
1979
1983
                      /* checking a geometry for validity */
1980
 
                      gaiaResetGeosMsg ();
1981
 
                      if (!gaiaIsValid (geom))
 
1984
                      int valret;
 
1985
                      if (p_cache != NULL)
 
1986
                        {
 
1987
                            gaiaResetGeosMsg_r (p_cache);
 
1988
                            valret = gaiaIsValid_r (p_cache, geom);
 
1989
                        }
 
1990
                      else
 
1991
                        {
 
1992
                            gaiaResetGeosMsg ();
 
1993
                            valret = gaiaIsValid (geom);
 
1994
                        }
 
1995
                      if (!valret)
1982
1996
                        {
1983
1997
                            unsigned char *blob_geom;
1984
1998
                            int blob_sz_geom;
2288
2302
    else if (report->n_repaired > 0)
2289
2303
      {
2290
2304
          fprintf (out,
2291
 
                   "\t\t\t<tr><td colspan=\"2\" class=\"ok\">This layer has been succesfully repaired and is now completely valid;<br>");
 
2305
                   "\t\t\t<tr><td colspan=\"2\" class=\"ok\">This layer has been successfully repaired and is now completely valid;<br>");
2292
2306
          fprintf (out,
2293
2307
                   "doesn't require any further corrective action.</td></tr>\n");
2294
2308
      }
2395
2409
    free_sanitize_report (report);
2396
2410
    if (out)
2397
2411
        fclose (out);
 
2412
        
 
2413
#endif /* end LWGEOM conditional */
 
2414
    
2398
2415
    return 0;
2399
2416
}
2400
2417
 
2401
2418
SPATIALITE_DECLARE int
2402
 
sanitize_all_geometry_columns (sqlite3 * sqlite,
2403
 
                               const char *tmp_prefix,
2404
 
                               const char *output_dir, int *x_not_repaired,
2405
 
                               char **err_msg)
 
2419
sanitize_geometry_column (sqlite3 * sqlite, const char *table, const char *geom,
 
2420
                          const char *tmp_table, const char *report_path,
 
2421
                          int *n_invalids, int *n_repaired, int *n_discarded,
 
2422
                          int *n_failures, char **err_msg)
 
2423
{
 
2424
    return sanitize_geometry_column_common (NULL, sqlite, table, geom,
 
2425
                                            tmp_table, report_path, n_invalids,
 
2426
                                            n_repaired, n_discarded, n_failures,
 
2427
                                            err_msg);
 
2428
}
 
2429
 
 
2430
SPATIALITE_DECLARE int
 
2431
sanitize_geometry_column_r (const void *p_cache, sqlite3 * sqlite,
 
2432
                            const char *table, const char *geom,
 
2433
                            const char *tmp_table, const char *report_path,
 
2434
                            int *n_invalids, int *n_repaired, int *n_discarded,
 
2435
                            int *n_failures, char **err_msg)
 
2436
{
 
2437
    return sanitize_geometry_column_common (p_cache, sqlite, table, geom,
 
2438
                                            tmp_table, report_path, n_invalids,
 
2439
                                            n_repaired, n_discarded, n_failures,
 
2440
                                            err_msg);
 
2441
}
 
2442
 
 
2443
static int
 
2444
sanitize_all_geometry_columns_common (const void *p_cache, sqlite3 * sqlite,
 
2445
                                      const char *tmp_prefix,
 
2446
                                      const char *output_dir,
 
2447
                                      int *x_not_repaired, char **err_msg)
2406
2448
{
2407
2449
/* attempts to repair invalid Geometries from all Geometry Columns */
2408
2450
    const char *sql;
2566
2608
                report = sqlite3_mprintf ("%s/lyr_%04d.html", output_dir, i);
2567
2609
                tmp_table =
2568
2610
                    sqlite3_mprintf ("%s%s_%s", tmp_prefix, table, geom);
2569
 
                ret =
2570
 
                    sanitize_geometry_column (sqlite, table, geom, tmp_table,
2571
 
                                              report, &n_invalids, &n_repaired,
2572
 
                                              &n_discarded, &n_failures,
2573
 
                                              err_msg);
 
2611
                if (p_cache != NULL)
 
2612
                    ret =
 
2613
                        sanitize_geometry_column_r (p_cache, sqlite, table,
 
2614
                                                    geom, tmp_table, report,
 
2615
                                                    &n_invalids, &n_repaired,
 
2616
                                                    &n_discarded, &n_failures,
 
2617
                                                    err_msg);
 
2618
                else
 
2619
                    ret =
 
2620
                        sanitize_geometry_column (sqlite, table, geom,
 
2621
                                                  tmp_table, report,
 
2622
                                                  &n_invalids, &n_repaired,
 
2623
                                                  &n_discarded, &n_failures,
 
2624
                                                  err_msg);
2574
2625
                sqlite3_free (report);
2575
2626
                sqlite3_free (tmp_table);
2576
2627
                fprintf (out,
2586
2637
                else if (n_discarded == 0 && n_failures == 0)
2587
2638
                  {
2588
2639
                      p_msg =
2589
 
                          "NONE: this layer has been succesfully sanitized and is now fully valid";
 
2640
                          "NONE: this layer has been successfully sanitized and is now fully valid";
2590
2641
                      cls_msg = "ok";
2591
2642
                  }
2592
2643
                else if (n_discarded == 0 && n_failures > 0)
2635
2686
    return 0;
2636
2687
}
2637
2688
 
 
2689
SPATIALITE_DECLARE int
 
2690
sanitize_all_geometry_columns (sqlite3 * sqlite,
 
2691
                               const char *tmp_prefix,
 
2692
                               const char *output_dir, int *x_not_repaired,
 
2693
                               char **err_msg)
 
2694
{
 
2695
    return sanitize_all_geometry_columns_common (NULL, sqlite, tmp_prefix,
 
2696
                                                 output_dir, x_not_repaired,
 
2697
                                                 err_msg);
 
2698
}
 
2699
 
 
2700
SPATIALITE_DECLARE int
 
2701
sanitize_all_geometry_columns_r (const void *p_cache, sqlite3 * sqlite,
 
2702
                                 const char *tmp_prefix,
 
2703
                                 const char *output_dir, int *x_not_repaired,
 
2704
                                 char **err_msg)
 
2705
{
 
2706
    return sanitize_all_geometry_columns_common (p_cache, sqlite, tmp_prefix,
 
2707
                                                 output_dir, x_not_repaired,
 
2708
                                                 err_msg);
 
2709
}
 
2710
 
2638
2711
#else /* LIBXML2 isn't enabled */
2639
2712
 
2640
2713
SPATIALITE_DECLARE int
2662
2735
}
2663
2736
 
2664
2737
SPATIALITE_DECLARE int
 
2738
sanitize_all_geometry_columns_r (const void *p_cache, sqlite3 * sqlite,
 
2739
                                 const char *tmp_prefix,
 
2740
                                 const char *output_dir, int *x_not_repaired,
 
2741
                                 char **err_msg)
 
2742
{
 
2743
/* LWGEOM isn't enabled: always returning an error */
 
2744
    int len;
 
2745
    const char *msg = "Sorry ... libspatialite was built disabling LWGEOM\n"
 
2746
        "and is thus unable to support MakeValid";
 
2747
 
 
2748
/* silencing stupid compiler warnings */
 
2749
    if (p_cache == NULL || sqlite == NULL || tmp_prefix == NULL
 
2750
        || output_dir == NULL || x_not_repaired == NULL)
 
2751
        tmp_prefix = NULL;
 
2752
 
 
2753
    if (err_msg == NULL)
 
2754
        return 0;
 
2755
    len = strlen (msg);
 
2756
    *err_msg = malloc (len + 1);
 
2757
    strcpy (*err_msg, msg);
 
2758
    return 0;
 
2759
}
 
2760
 
 
2761
SPATIALITE_DECLARE int
2665
2762
sanitize_geometry_column (sqlite3 * sqlite, const char *table, const char *geom,
2666
2763
                          const char *tmp_table, const char *report_path,
2667
2764
                          int *n_invalids, int *n_repaired, int *n_discarded,
2687
2784
    return 0;
2688
2785
}
2689
2786
 
 
2787
SPATIALITE_DECLARE int
 
2788
sanitize_geometry_column_r (const void *p_cache, sqlite3 * sqlite,
 
2789
                            const char *table, const char *geom,
 
2790
                            const char *tmp_table, const char *report_path,
 
2791
                            int *n_invalids, int *n_repaired, int *n_discarded,
 
2792
                            int *n_failures, char **err_msg)
 
2793
{
 
2794
/* LWGEOM isn't enabled: always returning an error */
 
2795
    int len;
 
2796
    const char *msg = "Sorry ... libspatialite was built disabling LWGEOM\n"
 
2797
        "and is thus unable to support MakeValid";
 
2798
 
 
2799
/* silencing stupid compiler warnings */
 
2800
    if (p_cache == NULL || sqlite == NULL || table == NULL || geom == NULL
 
2801
        || tmp_table == NULL || report_path == NULL || n_invalids == NULL
 
2802
        || n_repaired == NULL || n_discarded == NULL || n_failures == NULL)
 
2803
        table = NULL;
 
2804
 
 
2805
 
 
2806
    if (err_msg == NULL)
 
2807
        return 0;
 
2808
    len = strlen (msg);
 
2809
    *err_msg = malloc (len + 1);
 
2810
    strcpy (*err_msg, msg);
 
2811
    return 0;
 
2812
}
 
2813
 
2690
2814
#endif /* end LWGEOM conditionals */
2691
2815
 
2692
2816
 
2773
2897
    report->last = r;
2774
2898
}
2775
2899
 
2776
 
SPATIALITE_DECLARE int
2777
 
check_geometry_column (sqlite3 * sqlite, const char *table, const char *geom,
2778
 
                       const char *report_path, int *n_rows, int *n_invalids,
2779
 
                       char **err_msg)
 
2900
static int
 
2901
check_geometry_column_common (const void *p_cache, sqlite3 * sqlite,
 
2902
                              const char *table, const char *geom,
 
2903
                              const char *report_path, int *n_rows,
 
2904
                              int *n_invalids, char **err_msg)
2780
2905
{
2781
2906
/* checks a Geometry Column for validity */
2782
2907
    char *sql;
3011
3136
                      const char *error;
3012
3137
                      const char *warning;
3013
3138
                      const char *extra;
3014
 
                      gaiaResetGeosMsg ();
3015
 
                      valid = gaiaIsValid (geom);
3016
 
                      error = gaiaGetGeosErrorMsg ();
3017
 
                      warning = gaiaGetGeosWarningMsg ();
3018
 
                      extra = gaiaGetGeosAuxErrorMsg ();
 
3139
                      if (p_cache != NULL)
 
3140
                        {
 
3141
                            gaiaResetGeosMsg_r (p_cache);
 
3142
                            valid = gaiaIsValid_r (p_cache, geom);
 
3143
                            error = gaiaGetGeosErrorMsg_r (p_cache);
 
3144
                            warning = gaiaGetGeosWarningMsg_r (p_cache);
 
3145
                            extra = gaiaGetGeosAuxErrorMsg_r (p_cache);
 
3146
                        }
 
3147
                      else
 
3148
                        {
 
3149
                            gaiaResetGeosMsg ();
 
3150
                            valid = gaiaIsValid (geom);
 
3151
                            error = gaiaGetGeosErrorMsg ();
 
3152
                            warning = gaiaGetGeosWarningMsg ();
 
3153
                            extra = gaiaGetGeosAuxErrorMsg ();
 
3154
                        }
3019
3155
                      if (!valid || error || warning)
3020
3156
                          addMessageToValidityReport (report, rowid, valid,
3021
3157
                                                      error, warning, extra);
3282
3418
}
3283
3419
 
3284
3420
SPATIALITE_DECLARE int
3285
 
check_all_geometry_columns (sqlite3 * sqlite,
3286
 
                            const char *output_dir, int *x_invalids,
3287
 
                            char **err_msg)
 
3421
check_geometry_column (sqlite3 * sqlite, const char *table, const char *geom,
 
3422
                       const char *report_path, int *n_rows, int *n_invalids,
 
3423
                       char **err_msg)
 
3424
{
 
3425
    return check_geometry_column_common (NULL, sqlite, table, geom, report_path,
 
3426
                                         n_rows, n_invalids, err_msg);
 
3427
}
 
3428
 
 
3429
SPATIALITE_DECLARE int
 
3430
check_geometry_column_r (const void *p_cache, sqlite3 * sqlite,
 
3431
                         const char *table, const char *geom,
 
3432
                         const char *report_path, int *n_rows, int *n_invalids,
 
3433
                         char **err_msg)
 
3434
{
 
3435
    return check_geometry_column_common (p_cache, sqlite, table, geom,
 
3436
                                         report_path, n_rows, n_invalids,
 
3437
                                         err_msg);
 
3438
}
 
3439
 
 
3440
static int
 
3441
check_all_geometry_columns_common (const void *p_cache, sqlite3 * sqlite,
 
3442
                                   const char *output_dir, int *x_invalids,
 
3443
                                   char **err_msg)
3288
3444
{
3289
3445
/* checks all Geometry Columns for validity */
3290
3446
    const char *sql;
3432
3588
                const char *table = results[(i * columns) + 0];
3433
3589
                const char *geom = results[(i * columns) + 1];
3434
3590
                report = sqlite3_mprintf ("%s/lyr_%04d.html", output_dir, i);
3435
 
                ret =
3436
 
                    check_geometry_column (sqlite, table, geom, report, &n_rows,
3437
 
                                           &n_invalids, err_msg);
 
3591
                if (p_cache != NULL)
 
3592
                    ret =
 
3593
                        check_geometry_column_r (p_cache, sqlite, table, geom,
 
3594
                                                 report, &n_rows, &n_invalids,
 
3595
                                                 err_msg);
 
3596
                else
 
3597
                    ret =
 
3598
                        check_geometry_column (sqlite, table, geom, report,
 
3599
                                               &n_rows, &n_invalids, err_msg);
3438
3600
                sqlite3_free (report);
3439
3601
                fprintf (out,
3440
3602
                         "\t\t\t<tr><td align=\"center\"><a href=\"./lyr_%04d.html\">show</a></td>",
3472
3634
    return 0;
3473
3635
}
3474
3636
 
 
3637
SPATIALITE_DECLARE int
 
3638
check_all_geometry_columns (sqlite3 * sqlite,
 
3639
                            const char *output_dir, int *x_invalids,
 
3640
                            char **err_msg)
 
3641
{
 
3642
    return check_all_geometry_columns_common (NULL, sqlite, output_dir,
 
3643
                                              x_invalids, err_msg);
 
3644
}
 
3645
 
 
3646
SPATIALITE_DECLARE int
 
3647
check_all_geometry_columns_r (const void *p_cache, sqlite3 * sqlite,
 
3648
                              const char *output_dir, int *x_invalids,
 
3649
                              char **err_msg)
 
3650
{
 
3651
    return check_all_geometry_columns_common (p_cache, sqlite, output_dir,
 
3652
                                              x_invalids, err_msg);
 
3653
}
3475
3654
 
3476
3655
#else
3477
3656
 
3497
3676
}
3498
3677
 
3499
3678
SPATIALITE_DECLARE int
 
3679
check_all_geometry_columns_r (const void *p_cache, sqlite3 * sqlite,
 
3680
                              const char *output_dir, int *x_invalids,
 
3681
                              char **err_msg)
 
3682
{
 
3683
/* GEOS isn't enabled: always returning an error */
 
3684
    int len;
 
3685
    const char *msg = "Sorry ... libspatialite was built disabling LWGEOM\n"
 
3686
        "and is thus unable to support IsValid";
 
3687
/* silencing stupid compiler warnings */
 
3688
    if (p_cache == NULL || sqlite == NULL || output_dir == NULL
 
3689
        || x_invalids == NULL)
 
3690
        output_dir = NULL;
 
3691
 
 
3692
    if (err_msg == NULL)
 
3693
        return 0;
 
3694
    len = strlen (msg);
 
3695
    *err_msg = malloc (len + 1);
 
3696
    strcpy (*err_msg, msg);
 
3697
    return 0;
 
3698
}
 
3699
 
 
3700
SPATIALITE_DECLARE int
3500
3701
check_geometry_column (sqlite3 * sqlite,
3501
3702
                       const char *table,
3502
3703
                       const char *geom,
3510
3711
 
3511
3712
/* silencing stupid compiler warnings */
3512
3713
    if (sqlite == NULL || table == NULL || geom == NULL ||
3513
 
        ||report_path == NULL || n_rows == NULL || n_invalids == NULL)
 
3714
        report_path == NULL || n_rows == NULL || n_invalids == NULL)
 
3715
        table = NULL;
 
3716
 
 
3717
    if (err_msg == NULL)
 
3718
        return 0;
 
3719
    len = strlen (msg);
 
3720
    *err_msg = malloc (len + 1);
 
3721
    strcpy (*err_msg, msg);
 
3722
    return 0;
 
3723
}
 
3724
 
 
3725
SPATIALITE_DECLARE int
 
3726
check_geometry_column_r (const void *p_cache, sqlite3 * sqlite,
 
3727
                         const char *table,
 
3728
                         const char *geom,
 
3729
                         const char *report_path,
 
3730
                         int *n_rows, int *n_invalids, char **err_msg)
 
3731
{
 
3732
/* GEOS isn't enabled: always returning an error */
 
3733
    int len;
 
3734
    const char *msg = "Sorry ... libspatialite was built disabling GEOS\n"
 
3735
        "and is thus unable to support IsValid";
 
3736
 
 
3737
/* silencing stupid compiler warnings */
 
3738
    if (p_cache == NULL || sqlite == NULL || table == NULL || geom == NULL ||
 
3739
        report_path == NULL || n_rows == NULL || n_invalids == NULL)
3514
3740
        table = NULL;
3515
3741
 
3516
3742
    if (err_msg == NULL)