• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

SizeOf() problem - Generating ESRI *.SHP Files

Dear Experts...

I have a record structure thus:

  TMapESRIHeader = record
    FileCode: integer;
    Unused1:  integer;
    Unused2:  integer;
    Unused3:  integer;
    Unused4:  integer;
    Unused5:  integer;
    FileLength: integer;
    Version: integer;
    ShapeType: integer;
    xMin,yMin,xMax,yMax,zMin,zMax,mMin,mMax: double;
  end;

This is 9 Integers ( 9 X 4 = 36 Bytes) and 8 Doubles (8 x 8 = 64 Bytes) 100 Bytes

 

  Later in a function I declare this ...

  Header : TMapESRIHeader;


  And later, when I do this ...
      BlockWrite(fShp,Header,SizeOf(Header));
 
  I get a 104 Byte block written to my file???? I expect a 100 Byte Block.

 
Any clues on this or anyone prepared to let me have some source code for exporting SHP, SHX & DBF Files (ESRI Shape Files)

 
  Thanks, Fairport
0
Fairport
Asked:
Fairport
  • 7
  • 5
  • 2
  • +1
1 Solution
 
LischkeCommented:
Hi Fairport,

the reason why you get 104 bytes is that Delphi aligns the record members beginning with xMin different. Try it out in the IDE:

var
  T: TMapESRIHeader;


PChar(@T.ShapeType) - PChar(@T) is 32 as expectect. That is the position of the member ShapeType in the record.

BUT PChar(@T.xMin) - PChar(@T) is not 36 as you would expect (because of the size of ShapeType being 4) but 40! This is so because the next data type is "double" so the alignment will be made to match this size.

The solution for this problem is quite simple (if you really want to have the 100 bytes). Use the keyword "packed" for your record declaration.

Ciao, Mike
0
 
kretzschmarCommented:
hi fairport,

redefine your structure to

TMapESRIHeader = packed record
    FileCode: integer;
    Unused1:  integer;
    Unused2:  integer;
    Unused3:  integer;
    Unused4:  integer;
    Unused5:  integer;
    FileLength: integer;
    Version: integer;
    ShapeType: integer;
    xMin,yMin,xMax,yMax,zMin,zMax,mMin,mMax: double;
  end;

meikl
0
 
ITugayCommented:
try change declaration to

TMapESRIHeader = packed record
....


Cheers,
Igor.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
kretzschmarCommented:
hi mike :-)
0
 
kretzschmarCommented:
hi igor :-)
0
 
LischkeCommented:
Hi meikl :-)
0
 
LischkeCommented:
Das war ja jetzt richtig knapp. Eine Minute länger in der IDE rumprobiert und alles wäre schon gelaufen gewesen :-)

Ciao, Mike
0
 
kretzschmarCommented:
? mike, wieso ?
du weißt doch das ich nur kommentiere.
aber knapp war's scho ;-))
0
 
ITugayCommented:
Wirklich eine Minute! Ich werde geerschwert,  zu verstehen. Ich sehe einige Buchstaben nicht. Aber es ist hallo egal!

By,
Igor.
0
 
LischkeCommented:
Hey Igor, seems you understand german :-) Well, german Delphi programmers are a quite large fraction here at E-E...

Meikl, what I meant was that I wouldn't have been the first one here a minute later (and I have spent already a few minutes in the IDE to make sure my answer is correct).

Ciao, Mike
0
 
FairportAuthor Commented:
Thanks...
Vey Clear and to the point.
We had actually found the problem & reason by the next day ourselves... but the "PACKED" record was not something I knew about.

0
 
LischkeCommented:



               WHY



have you then rejected my anser??? I was not only the first one with the correct answer I provided you with additional information why the alignment is so and I spent time on YOUR problem, not mine.


Ich bin so sauer, ich kann es hier gar nicht ausdrücken ohne unhöflich zu werden.
0
 
kretzschmarCommented:
? just wondering also,
because mike has pointed
you also to use the packed record definition

but anyway thanks for accepting my comment as answer ;-)

mike, nicht sauer sein,
(ich weiß, ich hab gut reden,
nachdem ich der nutznießer bin.
ich denke, ich würde
auch ganz schön sauer sein)
hast aber mein vollstes verständnis und
meine hochachtung vor deiner inneren disziplin.

bled glofn

meikl
0
 
LischkeCommented:
I'm already beyond it..., seufz :-)

Ciao, Mike
0
 
FairportAuthor Commented:
Lischke...
V sorry!!

I tried to work out how to reverse my rejection yesterday...
Sincere apologies. If I could have found a way to reverse the rejection I would have given you the points.

This was my first time in EE... When I displayed the answer on my Browser... all I saw were your remarks to someone regarding being German or something... and a set of radio buttons that asked if I accepted or not... so I rejected... then soon afterwards I saw that your answer was ok but could not find a way to sort out the problem I had caused!!!

Again sincere aplogies. I won't make that mistake again.

If I can buy you a beer one day... send you a cheque in the mail...or??

Thanks for your answer -
0
 
LischkeCommented:
Ok, accepted. Although I prefer wine, a beer would not be bad :-)

Next time you 'll make it better, I'm sure.

Ciao, Mike
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now