Solved

SizeOf() problem - Generating ESRI *.SHP Files

Posted on 2000-03-16
16
335 Views
Last Modified: 2010-08-05
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
Comment
Question by:Fairport
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 10

Expert Comment

by:Lischke
ID: 2627159
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 2627161
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
 
LVL 9

Expert Comment

by:ITugay
ID: 2627162
try change declaration to

TMapESRIHeader = packed record
....


Cheers,
Igor.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 2627164
hi mike :-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2627168
hi igor :-)
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2627183
Hi meikl :-)
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2627188
Das war ja jetzt richtig knapp. Eine Minute länger in der IDE rumprobiert und alles wäre schon gelaufen gewesen :-)

Ciao, Mike
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2627258
? mike, wieso ?
du weißt doch das ich nur kommentiere.
aber knapp war's scho ;-))
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2627292
Wirklich eine Minute! Ich werde geerschwert,  zu verstehen. Ich sehe einige Buchstaben nicht. Aber es ist hallo egal!

By,
Igor.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2627367
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
 

Author Comment

by:Fairport
ID: 2631061
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
 
LVL 10

Expert Comment

by:Lischke
ID: 2631105



               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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2631272
? 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
 
LVL 10

Expert Comment

by:Lischke
ID: 2631760
I'm already beyond it..., seufz :-)

Ciao, Mike
0
 

Author Comment

by:Fairport
ID: 2632410
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
 
LVL 10

Expert Comment

by:Lischke
ID: 2634147
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question