7

I have two shapefiles 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??

2
  • sql query? You're using some kind of a database? You can easly do that kind of spatial queries using postgis
    – nickves
    Commented Sep 29, 2012 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
    Commented Sep 29, 2012 at 20:38

2 Answers 2

11

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"
3
  • 1
    +1 because this is ultra cool, but will it allow the OP to do the intersection/conflation stuff they mention? ::bows::
    – elrobis
    Commented Oct 17, 2013 at 18:04
  • @elrobis I have updated the answer, because you make me notice that the second question was unanswered. Thank you. Commented Oct 18, 2013 at 8:52
  • 1
    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> Commented Jun 25, 2014 at 10:41
5

I wrote some downloadable code for that. A simple python program to combine shapefiles in many ways: http://gistncase.blogspot.com.au/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)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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