The work transformed from shapefile to geojson file

Geojson provides several advantages compared to shapefiles:

  1. lightweight, text-based, and easily readable format that can be easily shared, transmitted and used on the web.

  2. It supports a wider range of data types compared to shapefile and can be used across multiple platforms and programming languages.

  3. Additionally, geojson files have smaller file sizes, making them easier to store and process. These benefits make geojson a popular choice for geographic information data storage and exchange.

Note: this post was arranged by ChatGPT

1. Python-based GeoJson manipulation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
## 1. Search specific data from raw data and load as Shapely object
fname = './india_state_geo.json'
with open(fname) as f:
data = json.load(f)
for i, c in enumerate(data['features']):
if c['properties']['NAME_1'] in ['Haryana', 'Uttar Pradesh', 'Punjab']:
print (i,c['properties']['NAME_1'] )
hary_shp = shape(data['features'][12]['geometry'])

## 2. Mask 2-d array by shapely object
import time
start = time.time()
from warnings import filterwarnings
filterwarnings(action='ignore', category=DeprecationWarning, message='`np.bool` is a deprecated alias')

xx_, yy_ = np.meshgrid(lon_sa,lat_sa)
mask_hary = shapely.vectorized.contains(hary_shp, xx_,yy_)

## 3. Merge of multiple selected polygons
from shapely.geometry import shape, MultiPolygon
import json
### 3.1 Load the GeoJSON data into a Python object
with open(fname) as f:
data = json.load(f)

### 3.2 Convert the polygon features into shapely objects
polygons = [shape(feature['geometry']) for feature in data['features']]

### 3.3 Merge the polygons using the union method
poly_index = [3,4,15,19,21,22,23,24,31,34]
merged_polygon = polygons[poly_index[0]]
for i,polygon in enumerate(polygons):
if i in poly_index[1:]:
merged_polygon = merged_polygon.union(polygon)

### 3.4 Create a MultiPolygon object if necessary
if type(merged_polygon) != MultiPolygon:
merged_polygon = MultiPolygon([merged_polygon])


## 4. Transforming Shapefile to Geojson
import fiona
import json
fname2 = './example.shp'
# Open the shapefile using fiona
with fiona.open(fname2) as src:
# Create a new GeoJSON file
with open( 'example.json', "w") as output:
# Write the GeoJSON representation of the shapefile to the file
output.write(json.dumps(list(src)))

## 5. Merge A with B
with open('A.json') as f:
A = json.load(f)
with open('B.json') as f:
B = json.load(f)

merged_polygon = A.union(shape(B[0]['geometry']))

2. Javascript-based Geojson visualization

1

Kommentare

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×