Last active
October 7, 2025 21:29
-
-
Save aspose-com-gists/2fc9674d6af76b2dce669433d920cd7f to your computer and use it in GitHub Desktop.
Convert SHP to JSON in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import aspose.gis as gis | |
from aspose.gis import VectorLayer, Drivers | |
from aspose.pycore import cast | |
# Define a class for converting Shapefiles (.shp) to JSON (.json) | |
class ShapefileToJsonConverter: | |
def __init__(self, data_dir, license_path): | |
# Store the directory containing data files | |
self.data_dir = data_dir | |
# Store the path to the Aspose.GIS license file | |
self.license_path = license_path | |
# Apply the Aspose license to unlock full functionality | |
self._apply_license() | |
def _apply_license(self): | |
"""Apply Aspose.GIS license to avoid evaluation limitations""" | |
license = gis.License() | |
license.set_license(self.license_path) | |
def convert(self, input_filename, output_filename): | |
"""Convert a .shp (Shapefile) to .json (JSON)""" | |
# Construct full input and output file paths | |
input_path = os.path.join(self.data_dir, input_filename) | |
output_path = os.path.join(self.data_dir, output_filename) | |
# Perform conversion from Shapefile to JSON using Aspose.GIS | |
VectorLayer.convert( | |
input_path, # Input file path | |
Drivers.shapefile, # Source format driver | |
output_path, # Output file path | |
Drivers.geo_json # Target format driver | |
) | |
# Print confirmation message | |
print(f"Converted '{input_filename}' to '{output_filename}' in {self.data_dir}") | |
# Example usage when running the script directly | |
if __name__ == "__main__": | |
# Folder where input/output files are stored | |
data_directory = "files" | |
# Path to Aspose.GIS license file | |
license_file = "license.lic" | |
# Input Shapefile name | |
input_file = "input.shp" | |
# Output JSON file name | |
output_file = "output.json" | |
# Create a converter instance | |
converter = ShapefileToJsonConverter(data_directory, license_file) | |
# Run the conversion | |
converter.convert(input_file, output_file) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment