Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Ciao,

I have two shape files that I want merge (overlay/intersect) having in the destination the attributes of the two sources file.

ogr2ogr file_merged.shp tl_2010_us_uac10_conus_albersUA.shp
ogr2ogr -update -append file_merged.shp tabblock2010_10_pophu.shp -nln file_merged

In this way I obtain the polygons of the two shapes, but I lose the attributes of the tabblock2010_10_pophu.shp.

My final aim is then make an sql statement where I can retrieve polygon in common in the both shapes.

Any suggestions??

Thanks in advance

share|improve this question
    
sql query? You're using some kind of a database? You can easly do that kind of spatial queries using postgis –  nickves Sep 29 '12 at 0:21
    
Thanks menjaraz, i will test it. Actually i think should be possible with ogr2ogr by using sql statement. I tried several combination but not i did not succeed. Let's see if somebody else reply. Ciao Giuseppe –  user10600 Sep 29 '12 at 20:38

2 Answers 2

i wrote some downloadable code for that. A simple python program for combine shapefiles in many ways: http://furiousgis.blogspot.it/2012/05/python-shapefile-merger-utility.html

By default performs the dbf schemes union and it adds an attribute filled with the source shapefile name, so you can recognize records.

This is the commandline for your needs:

python shapemerger.py -o file_merged.shp tl_2010_us_uac10_conus_albersUA.shp tabblock2010_10_pophu.shp

(you have to run it in a python environment with available ogr/gdal packages. I wrote some note about this above the code)

share|improve this answer

Using GDAL >= 1.10.0 and its OGR Virtual Format, we can write a VRT file named, for example, merge.vrt (see Example: Union layer (GDAL >= 1.10.0)):

<OGRVRTDataSource>
    <OGRVRTUnionLayer name="unionLayer">
        <OGRVRTLayer name="source1">
            <SrcDataSource>source1.shp</SrcDataSource>
        </OGRVRTLayer>
        <OGRVRTLayer name="source2">
            <SrcDataSource>source2.shp</SrcDataSource>
        </OGRVRTLayer>
    </OGRVRTUnionLayer>
</OGRVRTDataSource>

and simply execute in order to simply preserve the attributes of both SHPs:

ogr2ogr merge.shp merge.vrt

Instead, if you want also exclude duplicate geometries from the result:

ogr2ogr merge_distinct.shp merge.vrt -dialect sqlite -sql "SELECT DISTINCT geometry, * FROM merge"
share|improve this answer
1  
+1 because this is ultra cool, but will it allow the OP to do the intersection/conflation stuff they mention? ::bows:: –  elrobis Oct 17 '13 at 18:04
    
@elrobis I have updated the answer, because you make me notice that the second question was unanswered. Thank you. –  afalciano Oct 18 '13 at 8:52
    
Hi, great answer, just an update for the final ogr2ogr statement. instead of "* FROM merge" for your example you want "* FROM unionLayer", since it was defined as unionLayer in your vrt. Also for those unfamiliar with vrt's in ogr your name="source1" will have to be the same as the name of your shapefile, so: <OGRVRTLayer name="myshapefile"> <SrcDataSource>myshapefile.shp</SrcDataSource> –  Vesanto Jun 25 at 10:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.