操作方法
在某些情况下,使用 脚本创建 web 地图更为有利,因为可针对多个 web 地图进行编程。 使用 JSON 文件中指定的定义,ArcGIS API for Python 中的 gis.content.add 函数可用于创建 web 地图。
可以使用 GIS 模块中的 gis.content.add 函数基于 JSON 文件表示创建 web 地图。 有两种方法:
注: 定义 JSON 描述的过程更长。 但是,用户通过该方法可以修改脚本的 JSON 表示。
以下步骤介绍了如何通过使用 json 模块读取 JSON 文件来基于 JSON 文件创建 web 地图。
导入所需的模块。
from arcgis.gis import GIS from arcgis.mapping import WebMap import json
gis = GIS("https://machine.domain.com/portal","adminuser","adminpassword")
print ("Successfully Logged Into GIS: ", gis)
with open('/home/jupyter/test2.json') as json_data:
data = json.load(json_data)
item_properties_dict = {"type": "Web Map","title": "Test Map","tags": ["test","test1","test2"],"snippet":"This is a snippet", "text":data}
newmap = gis.content.add(item_properties = item_properties_dict)
newmap
以下 显示的是完整脚本。
from arcgis.gis import GIS
from arcgis.mapping import WebMap
import json
gis = GIS("https://machine.domain.com/portal","adminuser","adminpassword")
print ("Successfully Logged Into GIS: ", gis)
with open('/home/jupyter/test2.json') as json_data:
data = json.load(json_data)
item_properties_dict = {"type": "Web Map","title": "Test Map","tags": ["test","test1","test2"],"snippet":"This is a snippet", "text":data}
newmap = gis.content.add(item_properties = item_properties_dict)
newmap
data = {
"operationalLayers": [{
"id": "layer_2382",
"layerType": "ArcGISMapServiceLayer",
"url": "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer",
"visibility": True,
"opacity": 1,
"title": "Census Data",
"layers": [{
"id": 0,
"layerDefinition": {
"source": {
"type": "mapLayer",
"mapLayerId": 0
}
},
"name": "Census Block Points",
"minScale": 99999.99998945338,
"maxScale": 0,
"parentLayerId": -1,
"defaultVisibility": True
}, {
"id": 1,
"layerDefinition": {
"source": {
"type": "mapLayer",
"mapLayerId": 1
},
"drawingInfo": {
"renderer": {
"type": "simple",
"label": "",
"description": "",
"symbol": {
"color": [0, 0, 0, 0],
"outline": {
"color": [230, 230, 0, 255],
"width": 0.39975000000000005,
"type": "esriSLS",
"style": "esriSLSSolid"
},
"type": "esriSFS",
"style": "esriSFSSolid"
}
}
}
},
"name": "Census Block Group",
"minScale": 1000000,
"maxScale": 0,
"parentLayerId": -1,
"defaultVisibility": True
}, {
"id": 2,
"layerDefinition": {
"source": {
"type": "mapLayer",
"mapLayerId": 2
},
"drawingInfo": {
"renderer": {
"type": "simple",
"label": "",
"description": "",
"symbol": {
"color": [0, 0, 0, 0],
"outline": {
"color": [230, 230, 0, 255],
"width": 0.5625,
"type": "esriSLS",
"style": "esriSLSSolid"
},
"type": "esriSFS",
"style": "esriSFSSolid"
}
}
}
},
"name": "Detailed Counties",
"minScale": 0,
"maxScale": 0,
"parentLayerId": -1,
"defaultVisibility": True
}, {
"id": 3,
"layerDefinition": {
"source": {
"type": "mapLayer",
"mapLayerId": 3
},
"drawingInfo": {
"renderer": {
"type": "simple",
"label": "",
"description": "",
"symbol": {
"color": [0, 0, 0, 0],
"outline": {
"color": [230, 230, 0, 255],
"width": 2,
"type": "esriSLS",
"style": "esriSLSSolid"
},
"type": "esriSFS",
"style": "esriSFSSolid"
}
}
}
},
"name": "states",
"minScale": 0,
"maxScale": 0,
"parentLayerId": -1,
"defaultVisibility": True
}
]
}
],
"baseMap": {
"baseMapLayers": [{
"id": "labels",
"layerType": "ArcGISTiledMapServiceLayer",
"url": "https://services.arcgisonline.com/arcgis/rest/services/Canvas/World_Dark_Gray_Base/MapServer",
"visibility": True,
"opacity": 1,
"title": "World_Dark_Gray_Base"
}, {
"id": "base",
"layerType": "ArcGISTiledMapServiceLayer",
"url": "https://services.arcgisonline.com/arcgis/rest/services/Canvas/World_Dark_Gray_Reference/MapServer",
"visibility": True,
"opacity": 1,
"title": "World_Dark_Gray_Reference"
}
],
"title": "Basemap"
},
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
},
"authoringApp": "WebMapViewer",
"authoringAppVersion": "10.6.1",
"version": "2.11"
}
item_properties_dict = {"type": "Web Map","title": "Test Map","tags": ["test","test1","test2"],"snippet":"This is a snippet", "text":data}
newmap = gis.content.add(item_properties = item_properties_dict)
newmap
以下显示的是完整脚本。
from arcgis.gis import GIS
from arcgis.mapping import WebMap
gis = GIS("https://earl.esri.com/portal","siteadmin","siteadmin", verify_cert=False)
print ("Successfully Logged Into GIS: ", gis)
data = {
"operationalLayers": [{
"id": "layer_2382",
"layerType": "ArcGISMapServiceLayer",
"url": "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer",
"visibility": True,
"opacity": 1,
"title": "Census Data",
"layers": [{
"id": 0,
"layerDefinition": {
"source": {
"type": "mapLayer",
"mapLayerId": 0
}
},
"name": "Census Block Points",
"minScale": 99999.99998945338,
"maxScale": 0,
"parentLayerId": -1,
"defaultVisibility": True
}, {
"id": 1,
"layerDefinition": {
"source": {
"type": "mapLayer",
"mapLayerId": 1
},
"drawingInfo": {
"renderer": {
"type": "simple",
"label": "",
"description": "",
"symbol": {
"color": [0, 0, 0, 0],
"outline": {
"color": [230, 230, 0, 255],
"width": 0.39975000000000005,
"type": "esriSLS",
"style": "esriSLSSolid"
},
"type": "esriSFS",
"style": "esriSFSSolid"
}
}
}
},
"name": "Census Block Group",
"minScale": 1000000,
"maxScale": 0,
"parentLayerId": -1,
"defaultVisibility": True
}, {
"id": 2,
"layerDefinition": {
"source": {
"type": "mapLayer",
"mapLayerId": 2
},
"drawingInfo": {
"renderer": {
"type": "simple",
"label": "",
"description": "",
"symbol": {
"color": [0, 0, 0, 0],
"outline": {
"color": [230, 230, 0, 255],
"width": 0.5625,
"type": "esriSLS",
"style": "esriSLSSolid"
},
"type": "esriSFS",
"style": "esriSFSSolid"
}
}
}
},
"name": "Detailed Counties",
"minScale": 0,
"maxScale": 0,
"parentLayerId": -1,
"defaultVisibility": True
}, {
"id": 3,
"layerDefinition": {
"source": {
"type": "mapLayer",
"mapLayerId": 3
},
"drawingInfo": {
"renderer": {
"type": "simple",
"label": "",
"description": "",
"symbol": {
"color": [0, 0, 0, 0],
"outline": {
"color": [230, 230, 0, 255],
"width": 2,
"type": "esriSLS",
"style": "esriSLSSolid"
},
"type": "esriSFS",
"style": "esriSFSSolid"
}
}
}
},
"name": "states",
"minScale": 0,
"maxScale": 0,
"parentLayerId": -1,
"defaultVisibility": True
}
]
}
],
"baseMap": {
"baseMapLayers": [{
"id": "labels",
"layerType": "ArcGISTiledMapServiceLayer",
"url": "https://services.arcgisonline.com/arcgis/rest/services/Canvas/World_Dark_Gray_Base/MapServer",
"visibility": True,
"opacity": 1,
"title": "World_Dark_Gray_Base"
}, {
"id": "base",
"layerType": "ArcGISTiledMapServiceLayer",
"url": "https://services.arcgisonline.com/arcgis/rest/services/Canvas/World_Dark_Gray_Reference/MapServer",
"visibility": True,
"opacity": 1,
"title": "World_Dark_Gray_Reference"
}
],
"title": "Basemap"
},
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
},
"authoringApp": "WebMapViewer",
"authoringAppVersion": "10.6.1",
"version": "2.11"
}
item_properties_dict = {"type": "Web Map","title": "Test Map","tags": ["test","test1","test2"],"snippet":"This is a snippet", "text":data}
newmap = gis.content.add(item_properties = item_properties_dict)
newmap
文章 ID: 000019701
获取来自 ArcGIS 专家的帮助
立即开始聊天