qgsgeometry.cpp.patch

Patch to fix address alignment problem in qgsgeometry.cpp - Sam Gillingham, 2012-01-31 12:55 PM

Download (35.6 KB)

View differences:

src/core/qgsgeometry.cpp 2012-02-01 09:42:48.915797000 +1300
709 709
  double actdist = std::numeric_limits<double>::max();
710 710
  double x = 0;
711 711
  double y = 0;
712
  double *tempx, *tempy;
712
  double tempx, tempy;
713 713
  memcpy( &wkbType, ( mGeometry + 1 ), sizeof( int ) );
714 714
  beforeVertex = -1;
715 715
  afterVertex = -1;
......
720 720
    case QGis::WKBPoint25D:
721 721
    case QGis::WKBPoint:
722 722
    {
723
      x = *(( double * )( mGeometry + 5 ) );
724
      y = *(( double * )( mGeometry + 5 + sizeof( double ) ) );
723
      memcpy( &x, mGeometry + 5, sizeof( x ) );
724
      memcpy( &y, mGeometry + 5 + sizeof( double ), sizeof( y ) );
725 725
      actdist = point.sqrDist( x, y );
726 726
      vertexnr = 0;
727 727
      break;
......
734 734
    case QGis::WKBLineString:
735 735
    {
736 736
      unsigned char* ptr = mGeometry + 5;
737
      int* npoints = ( int* )ptr;
737
      int npoints;
738
      memcpy( &npoints, ptr, sizeof( npoints ) );
738 739
      ptr += sizeof( int );
739
      for ( int index = 0; index < *npoints; ++index )
740
      for ( int index = 0; index < npoints; ++index )
740 741
      {
741
        tempx = ( double* )ptr;
742
        memcpy( &tempx, ptr, sizeof( tempx ) );
742 743
        ptr += sizeof( double );
743
        tempy = ( double* )ptr;
744
        if ( point.sqrDist( *tempx, *tempy ) < actdist )
744
        memcpy( &tempy, ptr, sizeof( tempy ) );
745
        if ( point.sqrDist( tempx, tempy ) < actdist )
745 746
        {
746
          x = *tempx;
747
          y = *tempy;
748
          actdist = point.sqrDist( *tempx, *tempy );
747
          x = tempx;
748
          y = tempy;
749
          actdist = point.sqrDist( tempx, tempy );
749 750
          vertexnr = index;
750 751
          if ( index == 0 )//assign the rubber band indices
751 752
          {
......
755 756
          {
756 757
            beforeVertex = index - 1;
757 758
          }
758
          if ( index == ( *npoints - 1 ) )
759
          if ( index == ( npoints - 1 ) )
759 760
          {
760 761
            afterVertex = -1;
761 762
          }
......
776 777
      hasZValue = true;
777 778
    case QGis::WKBPolygon:
778 779
    {
779
      int* nrings = ( int* )( mGeometry + 5 );
780
      int* npoints;
780
      int nrings;
781
      memcpy( &nrings, mGeometry + 5, sizeof( nrings ) );
782
      int npoints;
781 783
      unsigned char* ptr = mGeometry + 9;
782
      for ( int index = 0; index < *nrings; ++index )
784
      for ( int index = 0; index < nrings; ++index )
783 785
      {
784
        npoints = ( int* )ptr;
786
        memcpy( &npoints, ptr, sizeof( npoints ) );
785 787
        ptr += sizeof( int );
786
        for ( int index2 = 0; index2 < *npoints; ++index2 )
788
        for ( int index2 = 0; index2 < npoints; ++index2 )
787 789
        {
788
          tempx = ( double* )ptr;
790
          memcpy( &tempx, ptr, sizeof( tempx ) );
789 791
          ptr += sizeof( double );
790
          tempy = ( double* )ptr;
791
          if ( point.sqrDist( *tempx, *tempy ) < actdist )
792
          memcpy( &tempy, ptr, sizeof( tempy ) );
793
          if ( point.sqrDist( tempx, tempy ) < actdist )
792 794
          {
793
            x = *tempx;
794
            y = *tempy;
795
            actdist = point.sqrDist( *tempx, *tempy );
795
            x = tempx;
796
            y = tempy;
797
            actdist = point.sqrDist( tempx, tempy );
796 798
            vertexnr = vertexcounter;
797 799
            //assign the rubber band indices
798 800
            if ( index2 == 0 )
799 801
            {
800
              beforeVertex = vertexcounter + ( *npoints - 2 );
802
              beforeVertex = vertexcounter + ( npoints - 2 );
801 803
              afterVertex = vertexcounter + 1;
802 804
            }
803
            else if ( index2 == ( *npoints - 1 ) )
805
            else if ( index2 == ( npoints - 1 ) )
804 806
            {
805 807
              beforeVertex = vertexcounter - 1;
806
              afterVertex = vertexcounter - ( *npoints - 2 );
808
              afterVertex = vertexcounter - ( npoints - 2 );
807 809
            }
808 810
            else
809 811
            {
......
831 833
      for ( int index = 0; index < *npoints; ++index )
832 834
      {
833 835
        ptr += ( 1 + sizeof( int ) ); //skip endian and point type
834
        tempx = ( double* )ptr;
835
        tempy = ( double* )( ptr + sizeof( double ) );
836
        if ( point.sqrDist( *tempx, *tempy ) < actdist )
837
        {
838
          x = *tempx;
839
          y = *tempy;
840
          actdist = point.sqrDist( *tempx, *tempy );
836
        memcpy( &tempx, ptr, sizeof( tempx ) );
837
        memcpy( &tempy, ptr + sizeof( double ), sizeof( tempy ) );
838
        if ( point.sqrDist( tempx, tempy ) < actdist )
839
        {
840
          x = tempx;
841
          y = tempy;
842
          actdist = point.sqrDist( tempx, tempy );
841 843
          vertexnr = index;
842 844
        }
843 845
        ptr += ( 2 * sizeof( double ) );
......
853 855
    case QGis::WKBMultiLineString:
854 856
    {
855 857
      unsigned char* ptr = mGeometry + 5;
856
      int* nlines = ( int* )ptr;
857
      int* npoints = 0;
858
      int nlines;
859
      memcpy( &nlines, ptr, sizeof( nlines ) );
860
      int npoints = 0;
858 861
      ptr += sizeof( int );
859
      for ( int index = 0; index < *nlines; ++index )
862
      for ( int index = 0; index < nlines; ++index )
860 863
      {
861 864
        ptr += ( sizeof( int ) + 1 );
862
        npoints = ( int* )ptr;
865
        memcpy( &npoints, ptr, sizeof( npoints ) );
863 866
        ptr += sizeof( int );
864
        for ( int index2 = 0; index2 < *npoints; ++index2 )
867
        for ( int index2 = 0; index2 < npoints; ++index2 )
865 868
        {
866
          tempx = ( double* )ptr;
869
          memcpy( &tempx, ptr, sizeof( tempx ) );
867 870
          ptr += sizeof( double );
868
          tempy = ( double* )ptr;
871
          memcpy( &tempy, ptr, sizeof( tempy ) );
869 872
          ptr += sizeof( double );
870
          if ( point.sqrDist( *tempx, *tempy ) < actdist )
873
          if ( point.sqrDist( tempx, tempy ) < actdist )
871 874
          {
872
            x = *tempx;
873
            y = *tempy;
874
            actdist = point.sqrDist( *tempx, *tempy );
875
            x = tempx;
876
            y = tempy;
877
            actdist = point.sqrDist( tempx, tempy );
875 878
            vertexnr = vertexcounter;
876 879

  
877 880
            if ( index2 == 0 )//assign the rubber band indices
......
882 885
            {
883 886
              beforeVertex = vertexnr - 1;
884 887
            }
885
            if ( index2 == ( *npoints ) - 1 )
888
            if ( index2 == ( npoints ) - 1 )
886 889
            {
887 890
              afterVertex = -1;
888 891
            }
......
905 908
    case QGis::WKBMultiPolygon:
906 909
    {
907 910
      unsigned char* ptr = mGeometry + 5;
908
      int* npolys = ( int* )ptr;
909
      int* nrings;
910
      int* npoints;
911
      int npolys;
912
      memcpy( &npolys, ptr, sizeof( npolys ) );
913
      int nrings;
914
      int npoints;
911 915
      ptr += sizeof( int );
912
      for ( int index = 0; index < *npolys; ++index )
916
      for ( int index = 0; index < npolys; ++index )
913 917
      {
914 918
        ptr += ( 1 + sizeof( int ) ); //skip endian and polygon type
915
        nrings = ( int* )ptr;
919
        memcpy( &nrings, ptr, sizeof( nrings ) );
916 920
        ptr += sizeof( int );
917
        for ( int index2 = 0; index2 < *nrings; ++index2 )
921
        for ( int index2 = 0; index2 < nrings; ++index2 )
918 922
        {
919
          npoints = ( int* )ptr;
923
          memcpy( &npoints, ptr, sizeof( npoints ) );
920 924
          ptr += sizeof( int );
921
          for ( int index3 = 0; index3 < *npoints; ++index3 )
925
          for ( int index3 = 0; index3 < npoints; ++index3 )
922 926
          {
923
            tempx = ( double* )ptr;
927
            memcpy( &tempx, ptr, sizeof( tempx ) );
924 928
            ptr += sizeof( double );
925
            tempy = ( double* )ptr;
926
            if ( point.sqrDist( *tempx, *tempy ) < actdist )
929
            memcpy( &tempy, ptr, sizeof( tempy ) ) ;
930
            if ( point.sqrDist( tempx, tempy ) < actdist )
927 931
            {
928
              x = *tempx;
929
              y = *tempy;
930
              actdist = point.sqrDist( *tempx, *tempy );
932
              x = tempx;
933
              y = tempy;
934
              actdist = point.sqrDist( tempx, tempy );
931 935
              vertexnr = vertexcounter;
932 936

  
933 937
              //assign the rubber band indices
934 938
              if ( index3 == 0 )
935 939
              {
936
                beforeVertex = vertexcounter + ( *npoints - 2 );
940
                beforeVertex = vertexcounter + ( npoints - 2 );
937 941
                afterVertex = vertexcounter + 1;
938 942
              }
939
              else if ( index3 == ( *npoints - 1 ) )
943
              else if ( index3 == ( npoints - 1 ) )
940 944
              {
941 945
                beforeVertex = vertexcounter - 1;
942
                afterVertex = vertexcounter - ( *npoints - 2 );
946
                afterVertex = vertexcounter - ( npoints - 2 );
943 947
              }
944 948
              else
945 949
              {
......
1002 1006
    case QGis::WKBLineString:
1003 1007
    {
1004 1008
      unsigned char* ptr = mGeometry + 5;
1005
      int* npoints = ( int* ) ptr;
1009
      int npoints;
1010
      memcpy( &npoints, ptr, sizeof( npoints ) );
1006 1011

  
1007 1012
      const int index = atVertex;
1008 1013

  
......
1017 1022
        beforeVertex = index - 1;
1018 1023
      }
1019 1024

  
1020
      if ( index == ( *npoints - 1 ) )
1025
      if ( index == ( npoints - 1 ) )
1021 1026
      {
1022 1027
        afterVertex = -1;
1023 1028
      }
......
1032 1037
      hasZValue = true;
1033 1038
    case QGis::WKBPolygon:
1034 1039
    {
1035
      int* nrings = ( int* )( mGeometry + 5 );
1036
      int* npoints;
1040
      int nrings;
1041
      memcpy( &nrings, mGeometry + 5, sizeof( nrings ) );
1042
      int npoints;
1037 1043
      unsigned char* ptr = mGeometry + 9;
1038 1044

  
1039 1045
      // Walk through the POLYGON WKB
1040 1046

  
1041
      for ( int index0 = 0; index0 < *nrings; ++index0 )
1047
      for ( int index0 = 0; index0 < nrings; ++index0 )
1042 1048
      {
1043
        npoints = ( int* )ptr;
1049
        memcpy( &npoints, ptr, sizeof( npoints ) );
1044 1050
        ptr += sizeof( int );
1045 1051

  
1046
        for ( int index1 = 0; index1 < *npoints; ++index1 )
1052
        for ( int index1 = 0; index1 < npoints; ++index1 )
1047 1053
        {
1048 1054
          ptr += sizeof( double );
1049 1055
          ptr += sizeof( double );
......
1055 1061
          {
1056 1062
            if ( index1 == 0 )
1057 1063
            {
1058
              beforeVertex = vertexcounter + ( *npoints - 2 );
1064
              beforeVertex = vertexcounter + ( npoints - 2 );
1059 1065
              afterVertex = vertexcounter + 1;
1060 1066
            }
1061
            else if ( index1 == ( *npoints - 1 ) )
1067
            else if ( index1 == ( npoints - 1 ) )
1062 1068
            {
1063 1069
              beforeVertex = vertexcounter - 1;
1064
              afterVertex = vertexcounter - ( *npoints - 2 );
1070
              afterVertex = vertexcounter - ( npoints - 2 );
1065 1071
            }
1066 1072
            else
1067 1073
            {
......
1087 1093
    case QGis::WKBMultiLineString:
1088 1094
    {
1089 1095
      unsigned char* ptr = mGeometry + 5;
1090
      int* nlines = ( int* )ptr;
1091
      int* npoints = 0;
1096
      int nlines;
1097
      memcpy( &nlines, ptr, sizeof( nlines ) );
1098
      int npoints = 0;
1092 1099
      ptr += sizeof( int );
1093 1100

  
1094
      for ( int index0 = 0; index0 < *nlines; ++index0 )
1101
      for ( int index0 = 0; index0 < nlines; ++index0 )
1095 1102
      {
1096 1103
        ptr += ( sizeof( int ) + 1 );
1097
        npoints = ( int* )ptr;
1104
        memcpy( &npoints, ptr, sizeof( npoints ) );
1098 1105
        ptr += sizeof( int );
1099 1106

  
1100
        for ( int index1 = 0; index1 < *npoints; ++index1 )
1107
        for ( int index1 = 0; index1 < npoints; ++index1 )
1101 1108
        {
1102 1109
          ptr += sizeof( double );
1103 1110
          ptr += sizeof( double );
......
1117 1124
            {
1118 1125
              beforeVertex = vertexcounter - 1;
1119 1126
            }
1120
            if ( index1 == ( *npoints ) - 1 )
1127
            if ( index1 == ( npoints ) - 1 )
1121 1128
            {
1122 1129
              afterVertex = -1;
1123 1130
            }
......
1136 1143
    case QGis::WKBMultiPolygon:
1137 1144
    {
1138 1145
      unsigned char* ptr = mGeometry + 5;
1139
      int* npolys = ( int* )ptr;
1140
      int* nrings;
1141
      int* npoints;
1146
      int npolys;
1147
      memcpy( &npolys, ptr, sizeof( npolys ) );
1148
      int nrings;
1149
      int npoints;
1142 1150
      ptr += sizeof( int );
1143 1151

  
1144
      for ( int index0 = 0; index0 < *npolys; ++index0 )
1152
      for ( int index0 = 0; index0 < npolys; ++index0 )
1145 1153
      {
1146 1154
        ptr += ( 1 + sizeof( int ) ); //skip endian and polygon type
1147
        nrings = ( int* )ptr;
1155
        memcpy( &nrings, ptr, sizeof( nrings ) );
1148 1156
        ptr += sizeof( int );
1149 1157

  
1150
        for ( int index1 = 0; index1 < *nrings; ++index1 )
1158
        for ( int index1 = 0; index1 < nrings; ++index1 )
1151 1159
        {
1152
          npoints = ( int* )ptr;
1160
          memcpy( &npoints, ptr, sizeof( npoints ) );
1153 1161
          ptr += sizeof( int );
1154 1162

  
1155
          for ( int index2 = 0; index2 < *npoints; ++index2 )
1163
          for ( int index2 = 0; index2 < npoints; ++index2 )
1156 1164
          {
1157 1165
            ptr += sizeof( double );
1158 1166
            ptr += sizeof( double );
......
1167 1175

  
1168 1176
              if ( index2 == 0 )
1169 1177
              {
1170
                beforeVertex = vertexcounter + ( *npoints - 2 );
1178
                beforeVertex = vertexcounter + ( npoints - 2 );
1171 1179
                afterVertex = vertexcounter + 1;
1172 1180
              }
1173
              else if ( index2 == ( *npoints - 1 ) )
1181
              else if ( index2 == ( npoints - 1 ) )
1174 1182
              {
1175 1183
                beforeVertex = vertexcounter - 1;
1176
                afterVertex = vertexcounter - ( *npoints - 2 );
1184
                afterVertex = vertexcounter - ( npoints - 2 );
1177 1185
              }
1178 1186
              else
1179 1187
              {
......
3435 3443
  double xmax = -std::numeric_limits<double>::max();
3436 3444
  double ymax = -std::numeric_limits<double>::max();
3437 3445

  
3438
  double *x;
3439
  double *y;
3440
  int *nPoints;
3441
  int *numRings;
3442
  int *numPolygons;
3446
  double x;
3447
  double y;
3448
  int nPoints;
3449
  int numRings;
3450
  int numPolygons;
3443 3451
  int numLineStrings;
3444 3452
  int idx, jdx, kdx;
3445 3453
  unsigned char *ptr;
......
3465 3473
  {
3466 3474
    case QGis::WKBPoint25D:
3467 3475
    case QGis::WKBPoint:
3468
      x = ( double * )( mGeometry + 5 );
3469
      y = ( double * )( mGeometry + 5 + sizeof( double ) );
3470
      if ( *x < xmin )
3476
      memcpy( &x, mGeometry + 5, sizeof( x ) );
3477
      memcpy( &y, mGeometry + 5 + sizeof( double ), sizeof( y ) );
3478
      if ( x < xmin )
3471 3479
      {
3472
        xmin = *x;
3480
        xmin = x;
3473 3481
      }
3474
      if ( *x > xmax )
3482
      if ( x > xmax )
3475 3483
      {
3476
        xmax = *x;
3484
        xmax = x;
3477 3485
      }
3478
      if ( *y < ymin )
3486
      if ( y < ymin )
3479 3487
      {
3480
        ymin = *y;
3488
        ymin = y;
3481 3489
      }
3482
      if ( *y > ymax )
3490
      if ( y > ymax )
3483 3491
      {
3484
        ymax = *y;
3492
        ymax = y;
3485 3493
      }
3486 3494
      break;
3487 3495
    case QGis::WKBMultiPoint25D:
......
3489 3497
    case QGis::WKBMultiPoint:
3490 3498
    {
3491 3499
      ptr = mGeometry + 1 + sizeof( int );
3492
      nPoints = ( int * ) ptr;
3500
      memcpy( &nPoints, ptr, sizeof( nPoints ) );
3493 3501
      ptr += sizeof( int );
3494
      for ( idx = 0; idx < *nPoints; idx++ )
3502
      for ( idx = 0; idx < nPoints; idx++ )
3495 3503
      {
3496
        ptr += ( 1 + sizeof( int ) );
3497
        x = ( double * ) ptr;
3504
        ptr +=1 + sizeof( int );
3505
        memcpy( &x, ptr, sizeof( x ) );
3498 3506
        ptr += sizeof( double );
3499
        y = ( double * ) ptr;
3507
        memcpy( &y, ptr, sizeof( y ) );
3500 3508
        ptr += sizeof( double );
3501 3509
        if ( hasZValue )
3502 3510
        {
3503 3511
          ptr += sizeof( double );
3504 3512
        }
3505
        if ( *x < xmin )
3513
        if ( x < xmin )
3506 3514
        {
3507
          xmin = *x;
3515
          xmin = x;
3508 3516
        }
3509
        if ( *x > xmax )
3517
        if ( x > xmax )
3510 3518
        {
3511
          xmax = *x;
3519
          xmax = x;
3512 3520
        }
3513
        if ( *y < ymin )
3521
        if ( y < ymin )
3514 3522
        {
3515
          ymin = *y;
3523
          ymin = y;
3516 3524
        }
3517
        if ( *y > ymax )
3525
        if ( y > ymax )
3518 3526
        {
3519
          ymax = *y;
3527
          ymax = y;
3520 3528
        }
3521 3529
      }
3522 3530
      break;
......
3527 3535
    {
3528 3536
      // get number of points in the line
3529 3537
      ptr = mGeometry + 5;
3530
      nPoints = ( int * ) ptr;
3538
      memcpy( &nPoints, ptr, sizeof( nPoints ) );
3531 3539
      ptr = mGeometry + 1 + 2 * sizeof( int );
3532
      for ( idx = 0; idx < *nPoints; idx++ )
3540
      for ( idx = 0; idx < nPoints; idx++ )
3533 3541
      {
3534
        x = ( double * ) ptr;
3542
        memcpy( &x, ptr, sizeof( x ) );
3535 3543
        ptr += sizeof( double );
3536
        y = ( double * ) ptr;
3544
        memcpy( &y, ptr, sizeof( y ) );
3537 3545
        ptr += sizeof( double );
3538 3546
        if ( hasZValue )
3539 3547
        {
3540 3548
          ptr += sizeof( double );
3541 3549
        }
3542
        if ( *x < xmin )
3550
        if ( x < xmin )
3543 3551
        {
3544
          xmin = *x;
3552
          xmin = x;
3545 3553
        }
3546
        if ( *x > xmax )
3554
        if ( x > xmax )
3547 3555
        {
3548
          xmax = *x;
3556
          xmax = x;
3549 3557
        }
3550
        if ( *y < ymin )
3558
        if ( y < ymin )
3551 3559
        {
3552
          ymin = *y;
3560
          ymin = y;
3553 3561
        }
3554
        if ( *y > ymax )
3562
        if ( y > ymax )
3555 3563
        {
3556
          ymax = *y;
3564
          ymax = y;
3557 3565
        }
3558 3566
      }
3559 3567
      break;
......
3568 3576
      {
3569 3577
        // each of these is a wbklinestring so must handle as such
3570 3578
        ptr += 5;   // skip type since we know its 2
3571
        nPoints = ( int * ) ptr;
3579
        memcpy( &nPoints, ptr, sizeof( nPoints ) );
3572 3580
        ptr += sizeof( int );
3573
        for ( idx = 0; idx < *nPoints; idx++ )
3581
        for ( idx = 0; idx < nPoints; idx++ )
3574 3582
        {
3575
          x = ( double * ) ptr;
3583
          memcpy( &x, ptr, sizeof( x ) );
3576 3584
          ptr += sizeof( double );
3577
          y = ( double * ) ptr;
3585
          memcpy( &y, ptr, sizeof( y ) );
3578 3586
          ptr += sizeof( double );
3579 3587
          if ( hasZValue )
3580 3588
          {
3581 3589
            ptr += sizeof( double );
3582 3590
          }
3583
          if ( *x < xmin )
3591
          if ( x < xmin )
3584 3592
          {
3585
            xmin = *x;
3593
            xmin = x;
3586 3594
          }
3587
          if ( *x > xmax )
3595
          if ( x > xmax )
3588 3596
          {
3589
            xmax = *x;
3597
            xmax = x;
3590 3598
          }
3591
          if ( *y < ymin )
3599
          if ( y < ymin )
3592 3600
          {
3593
            ymin = *y;
3601
            ymin = y;
3594 3602
          }
3595
          if ( *y > ymax )
3603
          if ( y > ymax )
3596 3604
          {
3597
            ymax = *y;
3605
            ymax = y;
3598 3606
          }
3599 3607
        }
3600 3608
      }
......
3605 3613
    case QGis::WKBPolygon:
3606 3614
    {
3607 3615
      // get number of rings in the polygon
3608
      numRings = ( int * )( mGeometry + 1 + sizeof( int ) );
3616
      ptr = mGeometry + 1 + sizeof( int );
3617
      memcpy( &numRings, ptr, sizeof( numRings ) );
3609 3618
      ptr = mGeometry + 1 + 2 * sizeof( int );
3610
      for ( idx = 0; idx < *numRings; idx++ )
3619
      for ( idx = 0; idx < numRings; idx++ )
3611 3620
      {
3612 3621
        // get number of points in the ring
3613
        nPoints = ( int * ) ptr;
3622
        memcpy( &nPoints, ptr, sizeof( nPoints ) );
3614 3623
        ptr += 4;
3615
        for ( jdx = 0; jdx < *nPoints; jdx++ )
3624
        for ( jdx = 0; jdx < nPoints; jdx++ )
3616 3625
        {
3617 3626
          // add points to a point array for drawing the polygon
3618
          x = ( double * ) ptr;
3627
          memcpy( &x, ptr, sizeof( x ) );
3619 3628
          ptr += sizeof( double );
3620
          y = ( double * ) ptr;
3629
          memcpy( &y, ptr, sizeof( y ) );
3621 3630
          ptr += sizeof( double );
3622 3631
          if ( hasZValue )
3623 3632
          {
3624 3633
            ptr += sizeof( double );
3625 3634
          }
3626
          if ( *x < xmin )
3635
          if ( x < xmin )
3627 3636
          {
3628
            xmin = *x;
3637
            xmin = x;
3629 3638
          }
3630
          if ( *x > xmax )
3639
          if ( x > xmax )
3631 3640
          {
3632
            xmax = *x;
3641
            xmax = x;
3633 3642
          }
3634
          if ( *y < ymin )
3643
          if ( y < ymin )
3635 3644
          {
3636
            ymin = *y;
3645
            ymin = y;
3637 3646
          }
3638
          if ( *y > ymax )
3647
          if ( y > ymax )
3639 3648
          {
3640
            ymax = *y;
3649
            ymax = y;
3641 3650
          }
3642 3651
        }
3643 3652
      }
......
3649 3658
    {
3650 3659
      // get the number of polygons
3651 3660
      ptr = mGeometry + 5;
3652
      numPolygons = ( int * ) ptr;
3661
      memcpy( &numPolygons, ptr, sizeof( numPolygons ) );
3653 3662
      ptr += 4;
3654 3663

  
3655
      for ( kdx = 0; kdx < *numPolygons; kdx++ )
3664
      for ( kdx = 0; kdx < numPolygons; kdx++ )
3656 3665
      {
3657 3666
        //skip the endian and mGeometry type info and
3658 3667
        // get number of rings in the polygon
3659 3668
        ptr += 5;
3660
        numRings = ( int * ) ptr;
3669
        memcpy( &numRings, ptr, sizeof( numRings ) );
3661 3670
        ptr += 4;
3662
        for ( idx = 0; idx < *numRings; idx++ )
3671
        for ( idx = 0; idx < numRings; idx++ )
3663 3672
        {
3664 3673
          // get number of points in the ring
3665
          nPoints = ( int * ) ptr;
3674
          memcpy( &nPoints, ptr, sizeof( nPoints ) );
3666 3675
          ptr += 4;
3667
          for ( jdx = 0; jdx < *nPoints; jdx++ )
3676
          for ( jdx = 0; jdx < nPoints; jdx++ )
3668 3677
          {
3669 3678
            // add points to a point array for drawing the polygon
3670
            x = ( double * ) ptr;
3679
            memcpy( &x, ptr, sizeof( x ) );
3671 3680
            ptr += sizeof( double );
3672
            y = ( double * ) ptr;
3681
            memcpy( &y, ptr, sizeof( y ) );
3673 3682
            ptr += sizeof( double );
3674 3683
            if ( hasZValue )
3675 3684
            {
3676 3685
              ptr += sizeof( double );
3677 3686
            }
3678
            if ( *x < xmin )
3687
            if ( x < xmin )
3679 3688
            {
3680
              xmin = *x;
3689
              xmin = x;
3681 3690
            }
3682
            if ( *x > xmax )
3691
            if ( x > xmax )
3683 3692
            {
3684
              xmax = *x;
3693
              xmax = x;
3685 3694
            }
3686
            if ( *y < ymin )
3695
            if ( y < ymin )
3687 3696
            {
3688
              ymin = *y;
3697
              ymin = y;
3689 3698
            }
3690
            if ( *y > ymax )
3699
            if ( y > ymax )
3691 3700
            {
3692
              ymax = *y;
3701
              ymax = y;
3693 3702
            }
3694 3703
          }
3695 3704
        }
......
3839 3848

  
3840 3849
  QGis::WkbType wkbType;
3841 3850
  bool hasZValue = false;
3842
  double *x, *y;
3851
  double x, y;
3843 3852

  
3844 3853
  QString mWkt; // TODO: rename
3845 3854

  
......
3853 3862
    case QGis::WKBPoint:
3854 3863
    {
3855 3864
      mWkt += "POINT(";
3856
      x = ( double * )( mGeometry + 5 );
3857
      mWkt += QString::number( *x, 'f', 6 );
3865
      memcpy( &x,  mGeometry + 5, sizeof( x ) );
3866
      mWkt += QString::number( x, 'f', 6 );
3858 3867
      mWkt += " ";
3859
      y = ( double * )( mGeometry + 5 + sizeof( double ) );
3860
      mWkt += QString::number( *y, 'f', 6 );
3868
      memcpy( &y, mGeometry + 5 + sizeof( double ), sizeof( y ) );
3869
      mWkt += QString::number( y, 'f', 6 );
3861 3870
      mWkt += ")";
3862 3871
      return mWkt;
3863 3872
    }
......
3868 3877
    {
3869 3878
      QgsDebugMsg( "LINESTRING found" );
3870 3879
      unsigned char *ptr;
3871
      int *nPoints;
3880
      int nPoints;
3872 3881
      int idx;
3873 3882

  
3874 3883
      mWkt += "LINESTRING(";
3875 3884
      // get number of points in the line
3876 3885
      ptr = mGeometry + 5;
3877
      nPoints = ( int * ) ptr;
3886
      memcpy( &nPoints, ptr, sizeof( nPoints ) );
3878 3887
      ptr = mGeometry + 1 + 2 * sizeof( int );
3879
      for ( idx = 0; idx < *nPoints; ++idx )
3888
      for ( idx = 0; idx < nPoints; ++idx )
3880 3889
      {
3881 3890
        if ( idx != 0 )
3882 3891
        {
3883 3892
          mWkt += ", ";
3884 3893
        }
3885
        x = ( double * ) ptr;
3886
        mWkt += QString::number( *x, 'f', 6 );
3894
        memcpy( &x, ptr, sizeof( x ) );
3895
        mWkt += QString::number( x, 'f', 6 );
3887 3896
        mWkt += " ";
3888 3897
        ptr += sizeof( double );
3889
        y = ( double * ) ptr;
3890
        mWkt += QString::number( *y, 'f', 6 );
3898
        memcpy( &y, ptr, sizeof( y ) );
3899
        mWkt += QString::number( y, 'f', 6 );
3891 3900
        ptr += sizeof( double );
3892 3901
        if ( hasZValue )
3893 3902
        {
......
3905 3914
      QgsDebugMsg( "POLYGON found" );
3906 3915
      unsigned char *ptr;
3907 3916
      int idx, jdx;
3908
      int *numRings, *nPoints;
3917
      int numRings, nPoints;
3909 3918

  
3910 3919
      mWkt += "POLYGON(";
3911 3920
      // get number of rings in the polygon
3912
      numRings = ( int * )( mGeometry + 1 + sizeof( int ) );
3913
      if ( !( *numRings ) )  // sanity check for zero rings in polygon
3921
      memcpy( &numRings, mGeometry + 1 + sizeof( int ), sizeof( numRings ) );
3922
      if ( !( numRings ) )  // sanity check for zero rings in polygon
3914 3923
      {
3915 3924
        return QString();
3916 3925
      }
3917 3926
      int *ringStart; // index of first point for each ring
3918 3927
      int *ringNumPoints; // number of points in each ring
3919
      ringStart = new int[*numRings];
3920
      ringNumPoints = new int[*numRings];
3928
      ringStart = new int[numRings];
3929
      ringNumPoints = new int[numRings];
3921 3930
      ptr = mGeometry + 1 + 2 * sizeof( int ); // set pointer to the first ring
3922
      for ( idx = 0; idx < *numRings; idx++ )
3931
      for ( idx = 0; idx < numRings; idx++ )
3923 3932
      {
3924 3933
        if ( idx != 0 )
3925 3934
        {
......
3927 3936
        }
3928 3937
        mWkt += "(";
3929 3938
        // get number of points in the ring
3930
        nPoints = ( int * ) ptr;
3931
        ringNumPoints[idx] = *nPoints;
3939
        memcpy( &nPoints, ptr, sizeof( nPoints ) );
3940
        ringNumPoints[idx] = nPoints;
3932 3941
        ptr += 4;
3933 3942

  
3934
        for ( jdx = 0; jdx < *nPoints; jdx++ )
3943
        for ( jdx = 0; jdx < nPoints; jdx++ )
3935 3944
        {
3936 3945
          if ( jdx != 0 )
3937 3946
          {
3938 3947
            mWkt += ",";
3939 3948
          }
3940
          x = ( double * ) ptr;
3941
          mWkt += QString::number( *x, 'f', 6 );
3949
          memcpy( &x, ptr, sizeof( x ) );
3950
          mWkt += QString::number( x, 'f', 6 );
3942 3951
          mWkt += " ";
3943 3952
          ptr += sizeof( double );
3944
          y = ( double * ) ptr;
3945
          mWkt += QString::number( *y, 'f', 6 );
3953
          memcpy( &y, ptr, sizeof( y ) );
3954
          mWkt += QString::number( y, 'f', 6 );
3946 3955
          ptr += sizeof( double );
3947 3956
          if ( hasZValue )
3948 3957
          {
......
3963 3972
    {
3964 3973
      unsigned char *ptr;
3965 3974
      int idx;
3966
      int *nPoints;
3975
      int nPoints;
3967 3976

  
3968 3977
      mWkt += "MULTIPOINT(";
3969
      nPoints = ( int* )( mGeometry + 5 );
3978
      memcpy( &nPoints, mGeometry + 5, sizeof( nPoints ) );
3970 3979
      ptr = mGeometry + 5 + sizeof( int );
3971
      for ( idx = 0; idx < *nPoints; ++idx )
3980
      for ( idx = 0; idx < nPoints; ++idx )
3972 3981
      {
3973 3982
        ptr += ( 1 + sizeof( int ) );
3974 3983
        if ( idx != 0 )
3975 3984
        {
3976 3985
          mWkt += ", ";
3977 3986
        }
3978
        x = ( double * )( ptr );
3979
        mWkt += QString::number( *x, 'f', 6 );
3987
        memcpy( &x, ptr, sizeof( x ) );
3988
        mWkt += QString::number( x, 'f', 6 );
3980 3989
        mWkt += " ";
3981 3990
        ptr += sizeof( double );
3982
        y = ( double * )( ptr );
3983
        mWkt += QString::number( *y, 'f', 6 );
3991
        memcpy( &y, ptr, sizeof( y ) );
3992
        mWkt += QString::number( y, 'f', 6 );
3984 3993
        ptr += sizeof( double );
3985 3994
        if ( hasZValue )
3986 3995
        {
......
3998 4007
      QgsDebugMsg( "MULTILINESTRING found" );
3999 4008
      unsigned char *ptr;
4000 4009
      int idx, jdx, numLineStrings;
4001
      int *nPoints;
4010
      int nPoints;
4002 4011

  
4003 4012
      mWkt += "MULTILINESTRING(";
4004 4013
      numLineStrings = ( int )( mGeometry[5] );
......
4011 4020
        }
4012 4021
        mWkt += "(";
4013 4022
        ptr += 5; // skip type since we know its 2
4014
        nPoints = ( int * ) ptr;
4023
        memcpy( &nPoints, ptr, sizeof( nPoints ) );
4015 4024
        ptr += sizeof( int );
4016
        for ( idx = 0; idx < *nPoints; idx++ )
4025
        for ( idx = 0; idx < nPoints; idx++ )
4017 4026
        {
4018 4027
          if ( idx != 0 )
4019 4028
          {
4020 4029
            mWkt += ", ";
4021 4030
          }
4022
          x = ( double * ) ptr;
4023
          mWkt += QString::number( *x, 'f', 6 );
4031
          memcpy( &x, ptr, sizeof( x ) );
4032
          mWkt += QString::number( x, 'f', 6 );
4024 4033
          ptr += sizeof( double );
4025 4034
          mWkt += " ";
4026
          y = ( double * ) ptr;
4027
          mWkt += QString::number( *y, 'f', 6 );
4035
          memcpy( &y, ptr, sizeof( y ) );
4036
          mWkt += QString::number( y, 'f', 6 );
4028 4037
          ptr += sizeof( double );
4029 4038
          if ( hasZValue )
4030 4039
          {
......
4044 4053
      QgsDebugMsg( "MULTIPOLYGON found" );
4045 4054
      unsigned char *ptr;
4046 4055
      int idx, jdx, kdx;
4047
      int *numPolygons, *numRings, *nPoints;
4056
      int numPolygons, numRings, nPoints;
4048 4057

  
4049 4058
      mWkt += "MULTIPOLYGON(";
4050 4059
      ptr = mGeometry + 5;
4051
      numPolygons = ( int * ) ptr;
4060
      memcpy( &numPolygons, ptr, sizeof( numPolygons ) );
4052 4061
      ptr = mGeometry + 9;
4053
      for ( kdx = 0; kdx < *numPolygons; kdx++ )
4062
      for ( kdx = 0; kdx < numPolygons; kdx++ )
4054 4063
      {
4055 4064
        if ( kdx != 0 )
4056 4065
        {
......
4058 4067
        }
4059 4068
        mWkt += "(";
4060 4069
        ptr += 5;
4061
        numRings = ( int * ) ptr;
4070
        memcpy( &numRings, ptr, sizeof( numRings ) );
4062 4071
        ptr += 4;
4063
        for ( idx = 0; idx < *numRings; idx++ )
4072
        for ( idx = 0; idx < numRings; idx++ )
4064 4073
        {
4065 4074
          if ( idx != 0 )
4066 4075
          {
4067 4076
            mWkt += ",";
4068 4077
          }
4069 4078
          mWkt += "(";
4070
          nPoints = ( int * ) ptr;
4079
          memcpy( &nPoints, ptr, sizeof( nPoints ) );
4071 4080
          ptr += 4;
4072
          for ( jdx = 0; jdx < *nPoints; jdx++ )
4081
          for ( jdx = 0; jdx < nPoints; jdx++ )
4073 4082
          {
4074 4083
            if ( jdx != 0 )
4075 4084
            {
4076 4085
              mWkt += ",";
4077 4086
            }
4078
            x = ( double * ) ptr;
4079
            mWkt += QString::number( *x, 'f', 6 );
4087
            memcpy( &x, ptr, sizeof( x ) );
4088
            mWkt += QString::number( x, 'f', 6 );
4080 4089
            ptr += sizeof( double );
4081 4090
            mWkt += " ";
4082
            y = ( double * ) ptr;
4083
            mWkt += QString::number( *y, 'f', 6 );
4091
            memcpy( &y, ptr, sizeof( y ) );
4092
            mWkt += QString::number( y, 'f', 6 );
4084 4093
            ptr += sizeof( double );
4085 4094
            if ( hasZValue )
4086 4095
            {
......
4125 4134
    return true;
4126 4135
  }
4127 4136

  
4128
  double *x;
4129
  double *y;
4130
  int *nPoints;
4131
  int *numRings;
4132
  int *numPolygons;
4137
  double x;
4138
  double y;
4139
  int nPoints;
4140
  int numRings;
4141
  int numPolygons;
4133 4142
  int numLineStrings;
4134 4143
  int idx, jdx, kdx;
4135 4144
  unsigned char *ptr;
......
4147 4156
      case QGis::WKBPoint25D:
4148 4157
      case QGis::WKBPoint:
4149 4158
      {
4150
        x = ( double * )( mGeometry + 5 );
4151
        y = ( double * )( mGeometry + 5 + sizeof( double ) );
4159
        memcpy( &x, mGeometry + 5, sizeof( x ) );
4160
        memcpy( &y, mGeometry + 5 + sizeof( double ), sizeof( x ) );
4152 4161

  
4153
        mGeos = createGeosPoint( QgsPoint( *x, *y ) );
4162
        mGeos = createGeosPoint( QgsPoint( x, y ) );
4154 4163
        mDirtyGeos = false;
4155 4164
        break;
4156 4165
      }
......
4162 4171
        QVector<GEOSGeometry *> points;
4163 4172

  
4164 4173
        ptr = mGeometry + 5;
4165
        nPoints = ( int * ) ptr;
4174
        memcpy( &nPoints, ptr, sizeof( nPoints ) );
4166 4175
        ptr = mGeometry + 1 + 2 * sizeof( int );
4167
        for ( idx = 0; idx < *nPoints; idx++ )
4176
        for ( idx = 0; idx < nPoints; idx++ )
4168 4177
        {
4169 4178
          ptr += ( 1 + sizeof( int ) );
4170
          x = ( double * ) ptr;
4179
          memcpy( &x, ptr, sizeof( x ) );
4171 4180
          ptr += sizeof( double );
4172
          y = ( double * ) ptr;
4181
          memcpy( &y, ptr, sizeof( y ) );
4173 4182
          ptr += sizeof( double );
4174 4183
          if ( hasZValue )
4175 4184
          {
4176 4185
            ptr += sizeof( double );
4177 4186
          }
4178
          points << createGeosPoint( QgsPoint( *x, *y ) );
4187
          points << createGeosPoint( QgsPoint( x, y ) );
4179 4188
        }
4180 4189
        mGeos = createGeosCollection( GEOS_MULTIPOINT, points );
4181 4190
        mDirtyGeos = false;
......
4191 4200
        QgsPolyline sequence;
4192 4201

  
4193 4202
        ptr = mGeometry + 5;
4194
        nPoints = ( int * ) ptr;
4203
        memcpy( &nPoints, ptr, sizeof( nPoints ) );
4195 4204
        ptr = mGeometry + 1 + 2 * sizeof( int );
4196
        for ( idx = 0; idx < *nPoints; idx++ )
4205
        for ( idx = 0; idx < nPoints; idx++ )
4197 4206
        {
4198
          x = ( double * ) ptr;
4207
          memcpy( &x, ptr, sizeof( x ) );
4199 4208
          ptr += sizeof( double );
4200
          y = ( double * ) ptr;
4209
          memcpy( &y, ptr, sizeof( y ) );
4201 4210
          ptr += sizeof( double );
4202 4211
          if ( hasZValue )
4203 4212
          {
4204 4213
            ptr += sizeof( double );
4205 4214
          }
4206 4215

  
4207
          sequence << QgsPoint( *x, *y );
4216
          sequence << QgsPoint( x, y );
4208 4217
        }
4209 4218
        mDirtyGeos = false;
4210 4219
        mGeos = createGeosLineString( sequence );
......
4224 4233

  
4225 4234
          // each of these is a wbklinestring so must handle as such
4226 4235
          ptr += 5;   // skip type since we know its 2
4227
          nPoints = ( int * ) ptr;
4236
          memcpy( &nPoints, ptr, sizeof( nPoints ) );
4228 4237
          ptr += sizeof( int );
4229
          for ( idx = 0; idx < *nPoints; idx++ )
4238
          for ( idx = 0; idx < nPoints; idx++ )
4230 4239
          {
4231
            x = ( double * ) ptr;
4240
            memcpy( &x, ptr, sizeof( x ) );
4232 4241
            ptr += sizeof( double );
4233
            y = ( double * ) ptr;
4242
            memcpy( &y, ptr, sizeof( y ) );
4234 4243
            ptr += sizeof( double );
4235 4244
            if ( hasZValue )
4236 4245
            {
4237 4246
              ptr += sizeof( double );
4238 4247
            }
4239
            sequence << QgsPoint( *x, *y );
4248
            sequence << QgsPoint( x, y );
4240 4249
          }
4241 4250
          lines << createGeosLineString( sequence );
4242 4251
        }
......
4252 4261
        QgsDebugMsgLevel( "Polygon found", 3 );
4253 4262

  
4254 4263
        // get number of rings in the polygon
4255
        numRings = ( int * )( mGeometry + 1 + sizeof( int ) );
4264
        memcpy( &numRings, mGeometry + 1 + sizeof( int ), sizeof( numRings ) );
4256 4265
        ptr = mGeometry + 1 + 2 * sizeof( int );
4257 4266

  
4258 4267
        QVector<GEOSGeometry*> rings;
4259 4268

  
4260
        for ( idx = 0; idx < *numRings; idx++ )
4269
        for ( idx = 0; idx < numRings; idx++ )
4261 4270
        {
4262 4271
          //QgsDebugMsg("Ring nr: "+QString::number(idx));
4263 4272

  
4264 4273
          QgsPolyline sequence;
4265 4274

  
4266 4275
          // get number of points in the ring
4267
          nPoints = ( int * ) ptr;
4276
          memcpy( &nPoints, ptr, sizeof( nPoints ) );
4268 4277
          ptr += 4;
4269
          for ( jdx = 0; jdx < *nPoints; jdx++ )
4278
          for ( jdx = 0; jdx < nPoints; jdx++ )
4270 4279
          {
4271 4280
            // add points to a point array for drawing the polygon
4272
            x = ( double * ) ptr;
4281
            memcpy( &x, ptr, sizeof( x ) );
4273 4282
            ptr += sizeof( double );
4274
            y = ( double * ) ptr;
4283
            memcpy( &y, ptr, sizeof( y ) );
4275 4284
            ptr += sizeof( double );
4276 4285
            if ( hasZValue )
4277 4286
            {
4278 4287
              ptr += sizeof( double );
4279 4288
            }
4280
            sequence << QgsPoint( *x, *y );
4289
            sequence << QgsPoint( x, y );
4281 4290
          }
4282 4291

  
4283 4292
          rings << createGeosLinearRing( sequence );
......
4297 4306

  
4298 4307
        // get the number of polygons
4299 4308
        ptr = mGeometry + 5;
4300
        numPolygons = ( int * ) ptr;
4309
        memcpy( &numPolygons, ptr, sizeof( numPolygons ) );
4301 4310
        ptr = mGeometry + 9;
4302
        for ( kdx = 0; kdx < *numPolygons; kdx++ )
4311
        for ( kdx = 0; kdx < numPolygons; kdx++ )
4303 4312
        {
4304 4313
          //QgsDebugMsg("Polygon nr: "+QString::number(kdx));
4305 4314
          QVector<GEOSGeometry*> rings;
......
4307 4316
          //skip the endian and mGeometry type info and
4308 4317
          // get number of rings in the polygon
4309 4318
          ptr += 5;
4310
          numRings = ( int * ) ptr;
4319
          memcpy( &numRings, ptr, sizeof( numRings ) );
4311 4320
          ptr += 4;
4312
          for ( idx = 0; idx < *numRings; idx++ )
4321
          for ( idx = 0; idx < numRings; idx++ )
4313 4322
          {
4314 4323
            //QgsDebugMsg("Ring nr: "+QString::number(idx));
4315 4324

  
4316 4325
            QgsPolyline sequence;
4317 4326

  
4318 4327
            // get number of points in the ring
4319
            nPoints = ( int * ) ptr;
4328
            memcpy( &nPoints, ptr, sizeof( nPoints ) );
4320 4329
            ptr += 4;
4321
            for ( jdx = 0; jdx < *nPoints; jdx++ )
4330
            for ( jdx = 0; jdx < nPoints; jdx++ )
4322 4331
            {
4323 4332
              // add points to a point array for drawing the polygon
4324
              x = ( double * ) ptr;
4333
              memcpy( &x, ptr, sizeof( x ) );
4325 4334
              ptr += sizeof( double );
4326
              y = ( double * ) ptr;
4335
              memcpy( &y, ptr, sizeof( y ) );
4327 4336
              ptr += sizeof( double );
4328 4337
              if ( hasZValue )
4329 4338
              {
4330 4339
                ptr += sizeof( double );
4331 4340
              }
4332
              sequence << QgsPoint( *x, *y );
4341
              sequence << QgsPoint( x, y );
4333 4342
            }
4334 4343

  
4335 4344
            rings << createGeosLinearRing( sequence );
......
5834 5843
  bool hasZValue = ( type == QGis::WKBMultiPoint25D );
5835 5844

  
5836 5845
  unsigned char* ptr = mGeometry + 5;
5837
  unsigned int nPoints = *(( int* )ptr );
5846
  unsigned int nPoints;
5847
  memcpy( &nPoints, ptr, sizeof( nPoints ) );
5838 5848
  ptr += 4;
5839 5849

  
5840 5850
  QgsMultiPoint points( nPoints );
......
5855 5865
  bool hasZValue = ( type == QGis::WKBMultiLineString25D );
5856 5866

  
5857 5867
  unsigned char* ptr = mGeometry + 5;
5858
  unsigned int numLineStrings = *(( int* )ptr );
5868
  unsigned int numLineStrings;
5869
  memcpy( &numLineStrings, ptr, sizeof( numLineStrings ) );
5859 5870
  ptr += 4;
5860 5871

  
5861 5872
  QgsMultiPolyline lines( numLineStrings );
......
5877 5888
  bool hasZValue = ( type == QGis::WKBMultiPolygon25D );
5878 5889

  
5879 5890
  unsigned char* ptr = mGeometry + 5;
5880
  unsigned int numPolygons = *(( int* )ptr );
5891
  unsigned int numPolygons;
5892
  memcpy( &numPolygons, ptr, sizeof( numPolygons ) );
5881 5893
  ptr += 4;
5882 5894

  
5883 5895
  QgsMultiPolygon polygons( numPolygons );