How can I create a new layer (shapefile) from every element in another layer?

I have point shapefile 'przystanki.shp' (in Polish mean bus stops) with 19 point and some attributes in table. What I want, is create for each stops new shapefile(or feature class in GDB).

I try:

import arcpy
arcpy.env.workspace = r'F:\Prog_GIS'
arcpy.env.overwriteOutput = True
przystanki = 'przystanek.shp'
arcpy.SplitByAttributes_analysis(przystanki, r"F:\Prog_GIS\test.gdb", ['nazwa'])

and I get 19 separate attributes tables in GBD (excellent!) with names by column 'nazwa' (in PL: names) BUT - without geometry. It's not solving my problem, but I think is good way.

What function can I use in next steps to add geometry?

enter image description here

Or maybe is there another workflow to solve this problem?

Of course I can select every element and export only selected data (that I do in job), but I try find solution to solve this problem with for example larger layer.

  • Welcome! Please take the tour to learn more about this Q&A site and how to ensure your question has all the required details for others to help you. Then edit your question to include the software version and error message. I think you can drop out of the loop and just use the SplitByAttributes tool.
    – smiller
    Apr 17 '19 at 18:42

The split by attributes tool does not require a loop; you can just specify the output location and the criteria for splitting, per the documentation: http://desktop.arcgis.com/en/arcmap/latest/tools/analysis-toolbox/split-by-attributes.htm

Your initial code using the loop would give an error due to arcpy.SplitByAttributes_analysis not returning a list (it may return a result object, but that is a different data type which cannot be parsed like a list.)

Removing from the loop -


The above code will likely give you an error because the workspace "Test" is not a proper workspace (e.g. FGDB). Assuming the GDB Test.gdb exists --

arcpy.SplitByAttributes_analysis(przystanki, r'F:/Prog_GIS/Test.gdb',['name'])

The above code would give a unique feature class for each feature or group of features with a unique value for the "name" field within the feature class named by the variable "przystanki". Note Python is case sensitive so ensure these are the actual field names, variable names, and GDB file. Note I additionally changed the file path slightly to use the r (raw text) string conversion.

Here's the official example from the Esri documentation:

# Description: Use the SplitByAttributes tool to split a feature class by unique values.

# Import required modules
import arcpy

# Set local variables
in_feature_class = 'c:/data/base.gdb/ecology'
target_workspace = 'c:/data/output.gdb'
fields = ['REGION', 'ECO_CODE']

arcpy.SplitByAttributes_analysis(in_feature_class, target_workspace, fields)

In this example, the feature class is split by unique combinations of "REGION" and "ECO_CODE" fields. e.g. "REGION" = "South" and "ECO_CODE" = 5 would be in one feature class, while "REGION" = "South" and "ECO_CODE" = 6 would be in a separate feature class.

EDIT: There are some reports that this does not return a feature class, but just a table, for example in 10.5, when working with shapefiles. A workaround is to move the input dataset into a Geodatabase before processing. You may also be able to use a different tool, referenced in the thread below.

See https://community.esri.com/thread/204460-split-by-attributes-in-105-is-only-outputting-tables-not-shapefiles

  • Firstly: thanks! Your solution works. but give me table in GDB for every row. But how can I add the geometry? Use another function or something else? Apr 17 '19 at 19:22
  • From the documentation and my own tests with this, you should get feature classes if your input is feature classes. Did you accidentally pass a table instead? Edit your question to include your inputs.
    – smiller
    Apr 17 '19 at 19:29
  • OK, i edited - in advance sorry for my English, i hope everything is understandable. Apr 17 '19 at 20:06
  • This appears to be an issue with the tool and shapefile inputs. Editing my answer -- per this try moving it into a GDB first. community.esri.com/thread/…
    – smiller
    Apr 17 '19 at 20:09
  • OK works! I import my bus stops to file GDB and then use the SplitByAttributes_analysis() functions. Thank You so much! My lesson from this is that creates loop and use Python is not always best and simples way. Apr 17 '19 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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