{"id":1816,"date":"2017-08-16T23:40:54","date_gmt":"2017-08-16T23:40:54","guid":{"rendered":"http:\/\/blogs.lincoln.ac.nz\/gis\/?p=1816"},"modified":"2023-05-07T03:41:41","modified_gmt":"2023-05-07T03:41:41","slug":"turning-the-tables-on-text","status":"publish","type":"post","link":"https:\/\/blogs.lincoln.ac.nz\/gis\/turning-the-tables-on-text\/","title":{"rendered":"Turning the Tables on Text"},"content":{"rendered":"<p><em>This post demonstrates how to use the field calculator to extract text values from a field and add them to another field.<\/em><\/p>\n<p>We join our programme already in progress&#8230;<\/p>\n<p>So here&#8217;s the thing &#8211; I&#8217;ve been doing some more analysis on the proximity of tobacco retailers to secondary schools, focusing on areas within 0 &#8211; 500 m of each school in Christchurch &#8211; all things we&#8217;ve covered in earlier posts.\u00a0 I used Network Analysis to create zones around the schools and then used a tool that joins together attributes from two layers based on where they are with respect to each other.\u00a0 In this case, I tabulated the number of retailers within each polygon.\u00a0 Here&#8217;s what the layers looks like:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/zones.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1817\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/zones.jpg\" alt=\"\" width=\"1434\" height=\"750\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/zones.jpg 1434w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/zones-300x157.jpg 300w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/zones-1024x536.jpg 1024w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/zones-768x402.jpg 768w\" sizes=\"auto, (max-width: 1434px) 100vw, 1434px\" \/><\/a><\/p>\n<p>My aim is to summarise the number of retailers in each zone by the schools so I&#8217;d like to join the attribute table to my schools layer so I have everything in one place, only I&#8217;ve got a problem &#8211; the attribute that holds the school name is different in the two layers:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/tables.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1818\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/tables.jpg\" alt=\"\" width=\"824\" height=\"638\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/tables.jpg 824w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/tables-300x232.jpg 300w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/tables-768x595.jpg 768w\" sizes=\"auto, (max-width: 824px) 100vw, 824px\" \/><\/a><\/p>\n<p>In the secondary schools layer, it&#8217;s just the school name (e.g. &#8220;Aranui High School&#8221;) but in the walking zones layer, it&#8217;s &#8220;Aranui High School : 0 &#8211; 500&#8221;.\u00a0 The &#8220;0 &#8211; 500&#8221; bit came from the network analysis layer and I can&#8217;t do much about that.\u00a0 In order to do a table join, I need the school names to be <strong>exactly<\/strong> the same.\u00a0 So what&#8217;s a GIS analyst to do?\u00a0 This calls for a bit of <a href=\"http:\/\/blogs.lincoln.ac.nz\/gis\/python-scripting-with-the-field-calculator\/\" target=\"_blank\" rel=\"noopener noreferrer\">field calculating<\/a>.\u00a0 I&#8217;d like to extract everything to the left of the colon and add that text to a new field.\u00a0 As a first step, I&#8217;ll add a new text attribute to my walking zones layer; I&#8217;ll call it SchoolName:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/NewField.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1819\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/NewField.jpg\" alt=\"\" width=\"1172\" height=\"343\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/NewField.jpg 1172w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/NewField-300x88.jpg 300w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/NewField-1024x300.jpg 1024w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/NewField-768x225.jpg 768w\" sizes=\"auto, (max-width: 1172px) 100vw, 1172px\" \/><\/a><\/p>\n<p>Next, right-click on the new field name and choose the Field Calculator:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/FC.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1820\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/FC.jpg\" alt=\"\" width=\"477\" height=\"574\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/FC.jpg 477w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/FC-249x300.jpg 249w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><\/a><\/p>\n<p>Normally, we use the Field Calculator to calculate or add values to fields but we can also use it to extract text or <a href=\"https:\/\/www.xkcd.com\/1279\/\" target=\"_blank\" rel=\"noopener noreferrer\">concatenate<\/a> (link together) multiple attributes.\u00a0 Here&#8217;s one way to remove the &#8220;0 &#8211; 500&#8221; bit.<\/p>\n<p>First tick &#8220;Show Codeblock&#8221;<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/codeblock.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1821\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/codeblock.jpg\" alt=\"\" width=\"479\" height=\"575\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/codeblock.jpg 479w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/codeblock-250x300.jpg 250w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/a><\/p>\n<p>This allows us to write a small snippet of Visual Basic (VB) code to do the job.\u00a0 Python users, note that you can tick the Python parser to use commands in that scripting language.\u00a0 We&#8217;ve got two new boxes now: &#8220;Pre-Logic Script Code&#8221; and &#8220;SchoolName =&#8221;.\u00a0 We can type our code in the Pre-Logic box and then actually run it on the SchoolName attribute in that box.\u00a0 I&#8217;ve set it up this way:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/code4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1825\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/code4.jpg\" alt=\"\" width=\"452\" height=\"215\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/code4.jpg 452w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/code4-300x143.jpg 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/a><\/p>\n<p>Allow me to translate.\u00a0 Take the value in the &#8220;Name&#8221; attribute field (from the WalkingZones layer) and split the text at the colon character.\u00a0 Take what&#8217;s to the left of the colon and refer to it as &#8220;newName&#8221;.\u00a0 The &#8220;newName(0)&#8221; in the &#8220;SchoolName =&#8221; box then runs the code for each record in the table (or only on selected records if there are any) and puts the extracted text in that field when I click OK.\u00a0 After running it, here&#8217;s what I&#8217;ve got in my Walking Zones table:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1826\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/output.jpg\" alt=\"\" width=\"1182\" height=\"639\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/output.jpg 1182w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/output-300x162.jpg 300w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/output-1024x554.jpg 1024w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/output-768x415.jpg 768w\" sizes=\"auto, (max-width: 1182px) 100vw, 1182px\" \/><\/a><\/p>\n<p>My SchoolName attribute values now match my NAME values in ChchSecondarySchools and I can proceed to a table join:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/join.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1827\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/join.jpg\" alt=\"\" width=\"426\" height=\"613\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/join.jpg 426w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/join-208x300.jpg 208w\" sizes=\"auto, (max-width: 426px) 100vw, 426px\" \/><\/a><\/p>\n<p>Et voila!\u00a0 Now I&#8217;ve got all the data I need in my secondary schools layer:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/joined.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1828\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/joined.jpg\" alt=\"\" width=\"1182\" height=\"642\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/joined.jpg 1182w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/joined-300x163.jpg 300w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/joined-1024x556.jpg 1024w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2017\/08\/joined-768x417.jpg 768w\" sizes=\"auto, (max-width: 1182px) 100vw, 1182px\" \/><\/a><\/p>\n<p>This is just a small taste of the types of things we can do with the Field Calculator.\u00a0 More can be found <a href=\"http:\/\/desktop.arcgis.com\/en\/arcmap\/10.3\/manage-data\/tables\/calculate-field-examples.htm\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a> if one is inclined towards such geeky level GIS stuff (counseling is available).<\/p>\n<p>C<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post demonstrates how to use the field calculator to extract text values from a field and add them to another field. We join our programme already in progress&#8230; So here&#8217;s the thing &#8211; I&#8217;ve been doing some more analysis on the proximity of tobacco retailers to secondary schools, focusing on areas within 0 &#8211; [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1816","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/posts\/1816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/comments?post=1816"}],"version-history":[{"count":1,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/posts\/1816\/revisions"}],"predecessor-version":[{"id":4131,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/posts\/1816\/revisions\/4131"}],"wp:attachment":[{"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/media?parent=1816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/categories?post=1816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/tags?post=1816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}