diff -r 720c26c5326f shapewrite.m --- a/shapewrite.m Thu Aug 25 21:17:37 2016 +0200 +++ b/shapewrite.m Thu Aug 25 22:06:30 2016 +0200 @@ -44,6 +44,7 @@ ## Author: Philip Nienhuis ## Created: 2014-12-30 ## 2015-06-26 Three fixes by Jan Heckman +## 2016-08-25 Some fixes by Martin Kunz function [status] = shapewrite (shp, fname, atts=[]) @@ -84,10 +85,10 @@ [pth, fnm, ext] = fileparts (fname); if (isempty (ext)) bname = fname; - fname = [fname ".shp"]; + fname = [fname,".shp"]; else - ## Later on bname.shx and bname.dbf will be read - bname = [pth fnm]; + ## Later on bname.shx and bname.dbf will be written + bname = fullfile (pth, fnm); endif ## Prepare a few things @@ -102,10 +103,6 @@ [shp.Y] = deal (shp.Lat); %% shp = rmfield (shp, {"Lat", "Lon"}); endif - ## "Point" need not have a BoundingBox field => add a dummy if not found - if (stype == 1 && ! ismember ("BoundingBox", fldn)) - [shp.BoundingBox] = deal (repmat (zeros (0, 0), numel (shp))); - endif endif ## Only now (after input checks) open .shp and .shx files & rewind just to be sure @@ -114,7 +111,7 @@ error ("shapewrite: shapefile %s can't be opened for writing\n", fname); endif fseek (fids, 0, "bof"); - fidx = fopen ([ bname ".shx" ], "w"); + fidx = fopen ([bname,".shx"], "w"); if (fidx < 0) error ("shapewrite: indexfile %s can't be opened for writing\n", fname); endif @@ -162,10 +159,10 @@ ## Simply write XY cordinates fwrite (fids, [shp(ishp).X shp(ishp).Y], "double"); ## Update overall BoundingBox - xMin = min (xMin, shp(ishp).BoundingBox(1, 1)); - xMax = max (xMax, shp(ishp).BoundingBox(1, 2)); - yMin = min (yMin, shp(ishp).BoundingBox(2, 1)); - yMax = max (yMax, shp(ishp).BoundingBox(2, 2)); + xMin = min (xMin, shp(ishp).X); + xMax = max (xMax, shp(ishp).X); + yMin = min (yMin, shp(ishp).Y); + yMax = max (yMax, shp(ishp).Y); case 8 ## MultiPoint ## Record index number @@ -236,8 +233,8 @@ ## Check for dbfwrite function if (isempty (which ("dbfwrite"))) - printf ("shaperead.m: dbfwrite function not found. No attributes written.\n"); - printf (" (io package 2.2.6+ installed and loaded?)\n"); + warning( strcat ("shaperead.m: dbfwrite function not found. No attributes", + " written.\n (io package 2.2.6+ installed and loaded?)")); return; endif @@ -268,3 +265,15 @@ endif endfunction + +%!test +%! gs = struct ("Geometry", {"Point"}, "Lon", {0,1}, "Lat", {1,0}); +%! att = struct ("Name", {"A","B"}, "Val", {2,3}); +%! file = tempname +%! [dir fName ext] = fileparts (file); +%! pkg load io; +%! assert (shapewrite (gs, file, att), 1); +%! assert (exist (fullfile (dir, [fName,".shx"])), 2) +%! assert (exist (fullfile (dir, [fName,".dbf"])), 2) +%! attR = dbfread (fullfile (dir, [fName,".dbf"])); +%! assert (strcmp (attR(2:3,2), {"A";"B"}), true(2,1)) %This fails because A and B are zero-padded \ No newline at end of file