Hello!

I have a shapefile (.shp) containing the Landsat grid coverage of South America. I also have 97 other shapefiles, each of which covers one of the Landsat grid cells (the 97 being a subset of the cells in the first shapefile). I managed to get R (via maptools) to perform a calculation on each of the 97 and put the results in a data frame. Among other things, this data frame contains a column for grid cell ID that matches up with a column in the Landsat grid shapefile. I used merge() to combine these and then saved a new shapefile, following the instructions at http://help.nceas.ucsb.edu/R:_Spatial#Append_a_second_set_of_attributes_to_a_spatial_data_file.27s_attribute_table. Here is the code I used:

	require(maptools)
	landsat1 = readShapePoly("landsat_grid")
	  # Make a backup
	  landsat_orig = landsat1
	landsat_2_spdf = merge(landsat1@data,newcalc,by.x="PR",by.y="pr2",all.x=TRUE,sort=FALSE)
	landsat1@data = landsat_2_spdf
	writeSpatialShape(landsat1, "methodA")

Looking at the new data with

	View(landsat1@data),

it seemed like everything went great. The ID's matched up perfectly, and I checked some of the calculated figures and they matched, too. The polygons from landsat_grid.shp that were not included in the 97 were assigned NA for the column with the calculated figures, appropriately.

Unfortunately, when I imported the new shapefile to my GIS program, all the tiles were rearranged. screenshot_expectedcoverage (http://tinyurl.com/3nbm3q7 ) shows fine-scale data for what I was expecting (Amazonia; imagine a polygon drawn around all the data there), and screenshot_methodA (http://tinyurl.com/3htvlga) shows what I got. (Only the 97 polygons are shown.) 

I tried changing sort to TRUE in the fourth line (and the output shapefile name to "methodB"), but the tiles are still messed up, just differently — see screenshot_methodB (http://tinyurl.com/3h95vr3).

In both shape files, the attribute tables look fine, but the tiles are just totally rearranged. They're not all scattered over the world, either. In screenshot_rearrangement (which shows all tiles, not just the 97) (http://tinyurl.com/6xfabvm), you can see that they cover South America perfectly — they all line up with the polygons from landsat_grid.shp (not shown).

What do I need to do to fix this?

Thanks very much in advance.



Sam Rabin
     Graduate student
         Princeton University
         Ecology & Evolutionary Biology






	[[alternative HTML version deleted]]

