1

I would like to convert a shapefile to geojson using python window in arcmap. I did not find any python code to do so. any idea?

I want to import my shape in a leaflet map but since there are too many shapefiles I want to automate the process.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 22, 2017 at 12:29
1

2 Answers 2

5

If you have ArcGIS Desktop 10.5+, then you can use the Features To JSON geoprocessing tool. Please mind that this tool didn't have the parameter geoJSON in previous versions.

arcpy.FeaturesToJSON_conversion(in_features="empty.gdb/roads", 
 out_json_file="roads_FeaturesToJSON.json", 
 format_json="NOT_FORMATTED", 
 include_z_values="NO_Z_VALUES", 
 include_m_values="NO_M_VALUES", 
 geoJSON="GEOJSON")

If you have ArcGIS Pro installed, then you can use the same tool since it has always had this parameter.

If you don't have Pro and are on ArcGIS Desktop 10.4 or earlier, you have two options:

1) Use private interface of arcpy.Geometry objects that can expose some of the information about the geometries in the form of GeoJSON and then do the rest on your own using json module:

g = arcpy.PointGeometry(arcpy.Point(45,45), arcpy.SpatialReference(4326))
g.__geo_interface__
{'coordinates': (45.0, 45.0), 'type': 'Point'}

2) Start looking for open-source geospatial Python packages such as ogr.

answered Jul 22, 2017 at 18:13
2

FeaturesToJSON_conversion function:

http://pro.arcgis.com/en/pro-app/tool-reference/conversion/features-to-json.htm

Example:

import arcpy
import os
arcpy.env.workspace = "c:/data"
arcpy.FeaturesToJSON_conversion(os.path.join("outgdb.gdb", "myfeatures"), "myjsonfeatures.json")
arcpy.FeaturesToJSON_conversion(os.path.join("outgdb.gdb", "myfeatures"), "mypjsonfeatures.json", "FORMATTED")
answered Jul 22, 2017 at 13:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.