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.
-
ArcGIS toolbox github.com/jasonbot/geojson-madnessuser2856– user28562017年07月23日 10:22:27 +00:00Commented Jul 23, 2017 at 10:22
2 Answers 2
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.
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")