Wiki source code of Map Data Importer
Last modified by RPG Research Xwiki Documents Administrator on 2023/09/27 23:09
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity}} | ||
| 2 | #if ("$!request.action" == "import") | ||
| 3 | #set ($importFile = $doc.getAttachment($request.filename)) | ||
| 4 | ## Check if the file exists | ||
| 5 | #if ("$!importFile" == '') | ||
| 6 | {{error}}The attached file does not exist{{/error}} | ||
| 7 | #else | ||
| 8 | #set ($json = $importFile.getContentAsString()) | ||
| 9 | #set ($data = $jsontool.fromString($json)) | ||
| 10 | ## Check if the required fields are in the JSON | ||
| 11 | #if ("$!data[0].itemLabel" == '' || "$!data[0].lat" == '' || "$!data[0].lon" == '') | ||
| 12 | {{error}}The required fields (itemLabel, lat, lon) do not exist in JSON file{{/error}} | ||
| 13 | #else | ||
| 14 | #set ($POINT_CLASS_NAME = 'Maps.Code.PointClass') | ||
| 15 | ## Create class with custom properties | ||
| 16 | #if ("$!request.className" != '') | ||
| 17 | #set ($NEW_CLASS_NAME = "${request.space}.${request.className}") | ||
| 18 | #set ($classDoc = $xwiki.getDocument($NEW_CLASS_NAME)) | ||
| 19 | #set ($newDocXWiki = $classDoc.getDocument()) | ||
| 20 | #set ($newClass = $newDocXWiki.getXClass()) | ||
| 21 | #set ($allCustomKeys = []) | ||
| 22 | #foreach ($key in $data[0].keySet()) | ||
| 23 | #if ($key != 'itemLabel' && $key != 'lat' && $key != 'lon') | ||
| 24 | #set ($discard = $allCustomKeys.add($key)) | ||
| 25 | #set ($keyPrettyName = "${key.substring(0, 1).toUpperCase()}${key.substring(1)}") | ||
| 26 | #set ($discard = $newClass.addTextField($key, $keyPrettyName, 100)) | ||
| 27 | #end | ||
| 28 | #end | ||
| 29 | #set ($discard = $classDoc.save()) | ||
| 30 | #end | ||
| 31 | #if ($data.size() > 0) | ||
| 32 | == Pages created == | ||
| 33 | #end | ||
| 34 | #set ($i = 0) | ||
| 35 | #foreach ($item in $data) | ||
| 36 | #if ($i < 100) | ||
| 37 | #set ($title = $item.itemLabel) | ||
| 38 | #if (!$title.matches('^Q\d*$')) | ||
| 39 | #set ($i = $i + 1) | ||
| 40 | #set ($page = $xwiki.getDocument("${request.space}.${title}")) | ||
| 41 | #if ("$!request.className" != '') | ||
| 42 | #set ($customPropsObj = $page.newObject($NEW_CLASS_NAME)) | ||
| 43 | #foreach ($key in $allCustomKeys) | ||
| 44 | #set ($discard = $customPropsObj.set($key, $item.get($key))) | ||
| 45 | #end | ||
| 46 | #end | ||
| 47 | #set ($pointObject = $page.newObject($POINT_CLASS_NAME)) | ||
| 48 | #set ($discard = $pointObject.set('latitude', "${item.lat}")) | ||
| 49 | #set ($discard = $pointObject.set('longitude', "${item.lon}")) | ||
| 50 | #set ($discard = $pointObject.set('includePopup', 1)) | ||
| 51 | #set ($docSave = $page.save()) | ||
| 52 | * [[$page.fullName]] | ||
| 53 | #end | ||
| 54 | #end | ||
| 55 | #end | ||
| 56 | #if ($data.size() > 0) | ||
| 57 | ---- | ||
| 58 | #end | ||
| 59 | #end | ||
| 60 | #end | ||
| 61 | #end | ||
| 62 | {{/velocity}} | ||
| 63 | |||
| 64 | {{info}} | ||
| 65 | Import map data from https://query.wikidata.org/ as JSON and add it to a map. | ||
| 66 | ==== Required fields ==== | ||
| 67 | **itemLabel, lon, lat** | ||
| 68 | ==== How does it work? ==== | ||
| 69 | * The JSON must include itemLabel, lon and lat for each map item. (SELECT ?itemLabel ?lon ?lat) | ||
| 70 | * Include all other custom fields by the end of the fields required. | ||
| 71 | * A class will be created for the custom fields and its objects will be attached to each map item page. | ||
| 72 | ==== Steps involved ==== | ||
| 73 | * Step 1: Head to https://query.wikidata.org/ and enter your SPARQL query. | ||
| 74 | * Step 2: Download the result as a JSON file and attach that file to this page. | ||
| 75 | * Step 3: Specify the name of the file attached that is to be imported. | ||
| 76 | * Step 4: Specify the space you want the new class and map items to be included inside. | ||
| 77 | * Step 5: Specify the name you want for the class to be created. (If the JSON has custom fields) | ||
| 78 | * Step 6: Press the import button. | ||
| 79 | {{/info}} | ||
| 80 | |||
| 81 | {{html}} | ||
| 82 | <form class="xform" action="" method="post"> | ||
| 83 | <dl> | ||
| 84 | <dt> | ||
| 85 | <label for="filename">Filename</label> | ||
| 86 | </dt> | ||
| 87 | <dd> | ||
| 88 | <input id="filename" name="filename" type="text" value="data.json"/> | ||
| 89 | </dd> | ||
| 90 | <dt> | ||
| 91 | <label for="className">Class Name</label> | ||
| 92 | </dt> | ||
| 93 | <dd> | ||
| 94 | <input id="className" name="className" type="text" value=""/> | ||
| 95 | </dd> | ||
| 96 | <dt> | ||
| 97 | <label for="space">Space</label> | ||
| 98 | </dt> | ||
| 99 | <dd> | ||
| 100 | <input id="space" name="space" type="text" value=""/> | ||
| 101 | </dd> | ||
| 102 | </dl> | ||
| 103 | <button class="btn btn-primary" type="submit" name="action" value="import">Import data</button> | ||
| 104 | </form> | ||
| 105 | {{/html}} |