Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
shapefile
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Sarah Abrishami
shapefile
Commits
0b275567
Commit
0b275567
authored
Apr 11, 2022
by
Sarah Abrishami
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
debugged center, added visualization file
parent
4eedffb2
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
13 deletions
+52
-13
create_multi_polygon.py
create_multi_polygon.py
+18
-13
output.json
output.json
+13
-0
visualization.py
visualization.py
+21
-0
No files found.
create_multi_polygon.py
View file @
0b275567
import
json
import
geopandas
as
gpd
from
shapely.geometry
import
Polygon
,
MultiPolygon
,
Point
import
fiona
def
find_center
(
mlp
):
def
find_center
(
mlp
,
how
=
'json'
):
if
isinstance
(
mlp
,
MultiPolygon
):
area
=
0
for
p
in
mlp
.
geoms
:
if
p
.
area
>
area
:
center
=
json
.
dumps
([
p
.
centroid
.
x
,
p
.
centroid
.
y
])
area
=
p
.
area
center
=
[
p
.
centroid
.
x
,
p
.
centroid
.
y
]
elif
isinstance
(
mlp
,
Polygon
):
center
=
json
.
dumps
([
mlp
.
centroid
.
x
,
mlp
.
centroid
.
y
])
center
=
[
mlp
.
centroid
.
x
,
mlp
.
centroid
.
y
]
else
:
raise
TypeError
(
'object not polygon nor multipolygon'
)
if
how
==
'json'
:
center
=
json
.
dumps
(
center
)
elif
how
==
'point'
:
center
=
Point
(
center
)
return
center
df
=
gpd
.
read_file
(
'Iran-Districts-Test.json'
)
df
=
df
.
loc
[
df
[
'geometry'
]
.
notna
()]
gdf
=
gpd
.
GeoDataFrame
(
columns
=
[
'geometry'
,
'clmd_id'
,
'center'
])
for
climate
in
df
[
'clmd_id'
]
.
unique
():
a
=
{
'geometry'
:
MultiPolygon
(),
'clmd_id'
:
climate
}
df
.
loc
[
df
[
'clmd_id'
]
==
climate
,
'geometry'
]
.
apply
(
lambda
x
:
a
.
update
({
'geometry'
:
a
[
'geometry'
]
.
union
(
x
)}))
gdf
=
gdf
.
append
(
a
,
ignore_index
=
True
)
if
__name__
==
'__main__'
:
df
=
gpd
.
read_file
(
'Iran-Districts-Test.json'
)
df
=
df
.
loc
[
df
[
'geometry'
]
.
notna
()]
gdf
=
gpd
.
GeoDataFrame
(
columns
=
[
'geometry'
,
'clmd_id'
,
'center'
])
for
climate
in
df
[
'clmd_id'
]
.
unique
():
a
=
{
'geometry'
:
MultiPolygon
(),
'clmd_id'
:
climate
}
df
.
loc
[
df
[
'clmd_id'
]
==
climate
,
'geometry'
]
.
apply
(
lambda
x
:
a
.
update
({
'geometry'
:
a
[
'geometry'
]
.
union
(
x
)}))
gdf
=
gdf
.
append
(
a
,
ignore_index
=
True
)
gdf
[
'center'
]
=
gdf
[
'geometry'
]
.
apply
(
find_center
)
gdf
.
to_file
(
'output.json'
,
driver
=
'GeoJSON'
)
gdf
[
'center'
]
=
gdf
[
'geometry'
]
.
apply
(
find_center
)
gdf
.
to_file
(
'output.json'
,
driver
=
'GeoJSON'
)
output.json
0 → 100644
View file @
0b275567
This diff is collapsed.
Click to expand it.
visualization.py
0 → 100644
View file @
0b275567
import
plotly.express
as
px
import
plotly.graph_objs
as
go
import
geopandas
as
gpd
from
create_multi_polygon
import
find_center
dist
=
gpd
.
read_file
(
'output.json'
)
dist
[
'center'
]
=
dist
[
'geometry'
]
.
apply
(
lambda
x
:
find_center
(
x
,
'point'
))
dist
[
'lat'
]
=
dist
[
'center'
]
.
y
dist
[
'lon'
]
=
dist
[
'center'
]
.
x
dist
.
append
(
gpd
.
GeoDataFrame
())
counties
=
{
'type'
:
'FeatureCollection'
,
'features'
:
list
(
dist
[[
'clmd_id'
,
'geometry'
]]
.
iterfeatures
())}
fig
=
go
.
Figure
(
go
.
Choroplethmapbox
(
geojson
=
counties
,
locations
=
dist
.
reset_index
()[
'index'
]
.
values
,
hovertext
=
dist
[
'clmd_id'
]
.
values
,
z
=
dist
[
'clmd_id'
]
.
values
,
colorscale
=
"Cividis_r"
,
zmin
=
0
))
fig
.
add_trace
(
px
.
scatter_mapbox
(
dist
,
lat
=
'lat'
,
lon
=
'lon'
,
hover_name
=
'clmd_id'
,
color_discrete_sequence
=
[
"blue"
])
.
data
[
0
])
fig
.
update_layout
(
mapbox_style
=
"carto-positron"
,
margin
=
{
"r"
:
0
,
"t"
:
0
,
"l"
:
0
,
"b"
:
0
},
mapbox_zoom
=
5
,
mapbox_center
=
{
"lat"
:
35.7
,
"lon"
:
51.4
})
fig
.
show
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment