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.

4

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.

1

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")

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.