1

I have a shapefile with 1475 records, and I want to generate a shapefile for each record.

What can I do to generate them avoiding selecting and exporting each record to do that?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Ariel
  • 315
  • 2
  • 13
  • 5
    That isn't a massive number of records! Do you have command line or python access to gdal? Can you show the schema (data types / fields) for your shapefile data? – BradHards May 26 '14 at 21:38
  • I understand some of code, but still I don't know python. You can have to the shapefile itself. The URL is this: http://www.dtpm.cl/index.php/2013-04-24-14-10-40/2013-04-26-17-44-02
    The name of the shapefile is Consolidado de Archivos Geográficos de Rutas -Shape - (12/04/14)
    – Ariel May 26 '14 at 21:48
  • 3
    consider http://gis.stackexchange.com/questions/25709/how-to-split-a-shapefile-into-separate-files-for-each-feature – deroses May 27 '14 at 01:37

1 Answers1

6

Let's assume you have access to Excel and this is a one-off and you don't want to (or are unwilling to) write a program.

Open the .dbf file in excel - yes, it's an Excel type just don't save it. What you want is just the FID field, but that's not there is it! You will notice that the records are arranged sequentially and consecutively (1-n with no breaks). Start a new worksheet and close the dbf, insert a series for the number of records (1-1475).

Insert a column before the series in the worksheet and copy this into it:

ogr2ogr -where FID=

Which is the OGR2OGR command. In this example I'm using a query filter which can adapt to any unique attribute or group of features. The other option is -fid .

in the column after the FID put your output shapefile and use your imagination for the output shape file, in this example I have used ="d:\OutPath\Out" & B1 & ".shp". The input shape file goes last.

FIRST ROW

fill the table by selecting the cells A, C & D for all the rows and use Ctrl + D to fill down. The fomula will change each output file:

FILLED ROWS

Now save as CSV, ignore the warnings, open with Notepad and you will get:

ogr2ogr -where FID=,1,d:\OutPath\Out1.shp,d:\some_path\InShape.shp
ogr2ogr -where FID=,2,d:\OutPath\Out2.shp,d:\some_path\InShape.shp
ogr2ogr -where FID=,3,d:\OutPath\Out3.shp,d:\some_path\InShape.shp
ogr2ogr -where FID=,4,d:\OutPath\Out4.shp,d:\some_path\InShape.shp
ogr2ogr -where FID=,5,d:\OutPath\Out5.shp,d:\some_path\InShape.shp
ogr2ogr -where FID=,6,d:\OutPath\Out6.shp,d:\some_path\InShape.shp
ogr2ogr -where FID=,7,d:\OutPath\Out7.shp,d:\some_path\InShape.shp
ogr2ogr -where FID=,8,d:\OutPath\Out8.shp,d:\some_path\InShape.shp
ogr2ogr -where FID=,9,d:\OutPath\Out9.shp,d:\some_path\InShape.shp

Do a find & replace FID=, with FID=, then , with space and you will get this:

ogr2ogr -where  FID=1 d:\OutPath\Out1.shp d:\some_path\InShape.shp
ogr2ogr -where  FID=2 d:\OutPath\Out2.shp d:\some_path\InShape.shp
ogr2ogr -where  FID=3 d:\OutPath\Out3.shp d:\some_path\InShape.shp
ogr2ogr -where  FID=4 d:\OutPath\Out4.shp d:\some_path\InShape.shp
ogr2ogr -where  FID=5 d:\OutPath\Out5.shp d:\some_path\InShape.shp
ogr2ogr -where  FID=6 d:\OutPath\Out6.shp d:\some_path\InShape.shp
ogr2ogr -where  FID=7 d:\OutPath\Out7.shp d:\some_path\InShape.shp
ogr2ogr -where  FID=8 d:\OutPath\Out8.shp d:\some_path\InShape.shp
ogr2ogr -where  FID=9 d:\OutPath\Out9.shp d:\some_path\InShape.shp

Which is batch commands! Save the file with the extension .bat and then double click to run.

The other way of exporting using -FID looks like this:

ogr2ogr -FID 1 d:\OutPath\Out1.shp d:\some_path\InShape.shp
ogr2ogr -FID 2 d:\OutPath\Out2.shp d:\some_path\InShape.shp
ogr2ogr -FID 3 d:\OutPath\Out3.shp d:\some_path\InShape.shp
ogr2ogr -FID 4 d:\OutPath\Out4.shp d:\some_path\InShape.shp
ogr2ogr -FID 5 d:\OutPath\Out5.shp d:\some_path\InShape.shp
ogr2ogr -FID 6 d:\OutPath\Out6.shp d:\some_path\InShape.shp
ogr2ogr -FID 7 d:\OutPath\Out7.shp d:\some_path\InShape.shp
ogr2ogr -FID 8 d:\OutPath\Out8.shp d:\some_path\InShape.shp
ogr2ogr -FID 9 d:\OutPath\Out9.shp d:\some_path\InShape.shp

traps Paths with spaces must be enclosed in "", so D:\project data\FromShape.shp becomes "d:\project data\FromShape.shp". OGR2OGR comes with QGIS but you might need to do some changes to get it to work. If the computer can't find the program insert the line set path=C:\Program Files\QGIS Dufour\bin;%path% in the top of the batch file... obviously substitute your own path to QGIS\bin folder, this one is for Dufour.

Michael Stimson
  • 25,566
  • 2
  • 35
  • 74
  • Thank you, It run, but it didn't show me any result. But, I found another way to do it. On the processing toolbox of QGIS, there is a script called Split vector layer by attribute. That script works very well. – Ariel May 30 '14 at 15:51
  • 1
    That's a shame. It worked for me; I did notice that if there is something wrong with the -where syntax it ran without error messages but created no output. It is distinctly possible that the split vector layer by attribute is merely a GUI for this process or very similar. – Michael Stimson May 31 '14 at 02:01