Automating Text Based Field Calculator Operations using Python Scripts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to another ArcGIS tutorial in today's tutorial I'm going to show you how to use the field calculator of ArcGIS to perform various tasks that you might need to do with the attributes table of a shapefile now as you can see I have already loaded up two shapefiles the map of United Kingdom and if i zoom in over here you can see the second shapefile is the railway the rail track network of United Kingdom so if I open the attributes table of both of the layers for example if I go to the United Kingdom shapefile and open the attributes table you can see that we just have one row but if I go to the second shapefile and open the attributes table you can see that we have a couple of columns over here the IES or country which specifies the name of the country and we also have a rail ID we have the type of a railroad and also we have some description so in this tutorial first I'm going to show you how to perform some basic text based operations and in the second part of the tutorial we will move on to value based operations so first of all what I would like to show you is I would like to show you how to replace this text for example if you consider this column which contains the type of the rail track now just imagine a hypothetical case where you need to replace this word railroad two rail tracks now how would you do that one way to do is actually to manually go to the editing mode and start typing rail tracks rail tracks rail tracks like that but as you might guess that's going to be a very tedious task and that's not very efficient at all so that's what we are trying to automate actually using these scripting methods so first what I'm going to do is I'm going to actually create a new column and this column I'm going to name it as test one and the type of the of the column is going to be text because now I'm going to perform some text-based operations and the length let's leave the length to be something like 100 so for the very first part of this tutorial as I told you before I'm going to show you how to replace this word railroad by a new word called rail tracks in this column so how do we do that we can first actually go to the editor mode and start editing and now if you double click over here you can even simply type it over here rail tracks but this is obviously not the way that I'm intending to do that so I'm just going to click over here on the heading of this column right click and go to field calculator alright once you reach this field calculator first make sure that your parser you're changing it from vbscript to Python and next what I'm going to do is I'm going to select this column over here which is named as type and an even under the fields box over here you can see that it already mentioned all the columns that you have in your attributes table already so I'm just going to make this test one column equal to type double click on this one dot free place and within brackets I'm specifying the old name that I can already see in the type column which is railroad and then I'm going to replace it by my new word which is rail tracks okay that's it and you can finally go ahead and click okay and now you can see that in our new column the word railroad got replaced by the word rail tracks now if you wish to go ahead and delete this column you can actually right click on it and then you can delete this column after you exit the editing more editor mode so next I would like to do a similar example for example let's say that if I would like to replace this capital R by a simple R so the way to do that is simply go to your field calculator again and this command will remain as same that means you are going to select your column type and you and you're going to replace and what I what exactly are you going to replace you're going to replace the capital R by a simple R so if you press ok you can see that the text format of the whole word got sort of changed into into simple letters so next I would like to show you how to select a specific part of a word for example if I would like to look at this column and if I would like to only isolate this the first three letters of this United Kingdom word the way to do that would be you can go to field calculator and now I'll remove the whole thing and now my test one column is going to be equal to this column which is I so country but I'm going to specify only only a certain number of characters for example if I type 0 here within square brackets and let's click OK you can see that it only selected the very first letter but now if I go to the field calculator again and if I enter 0 colon 3 and I think it would be a good time to recall now in Python the index number starts from zero and not from one for example if I would like to if I type the same word over here u n i-- t-- e d the index value of my first letter would be zero one two three four five so now you can see that when i specified so now you can see that when i specified my index from 0 up to 3 it only printed the the letters starting from 0 up to 2 so you can see actually the the character which corresponds to our third index did not get printed so in case if you need to get the whole thing printed for example if you need the whole word united printed over here in this test one column you have to specify your characters to be from you have to specify your index number to be from 0 up to 6 so that it will actually spring the corresponding characters of index number 0 1 2 3 4 5 now let's go ahead and try that so in the 6 over here and it should print united we successfully printed the word united so that's how you actually select a particular set of characters of your choice using a command in the field calculator now for example if you if you need to select let's say the the last four characters you can also do that by simply replacing this I so country and within brackets as I mentioned you just as I mentioned before if you need four characters you have to enter five but now since you need the last five characters you have to enter that with a negative value and semicolon now you can see you sort of printed the last five characters so that's how that works and now let's learn how to combine two different strings so for example if I would like to do something like this I would like to combine this description column along with this type column for example in that case I would have something like operational operational railroad now you can have that operational railroad by combining the first column and sorry you can you can have that by combining this descript column with type column but the description come first and the type should come last so the way to do that is you can go to the field calculator and first you select the column that you would like to combine by double clicking on that so in our case I would like to have my desk script column first and then you can put a plus sign and you can combine it with the type column right now let's click OK and see what happens right now you can see that it got combined we call that in Python concatenation but now the problem is you can see that here between the two words there is no space but I would actually like to have a space between the two words so that it it's easy to read that so the way to do that is you can still use the same argument but over here just simply pass a space over here and put a plus sign and then add your type column now if I click OK you can see that it got spaced out over here if you need the space to be a little bit more what you can do is you can actually put another space in here and then click ok as you can see it got spaced out even more so that's pretty straightforward so finally for the text-based operations I would like to show you how to capitalize words when you have more than one word for example if I see over here under the iso country column i have a word called united kingdom all in caps but i would like to have that changed to be something like united kingdom with the first letter being the capital with the first letter being capital and the rest being simple so the way to do that would be you can go to field calculator get rid of this one and over here make sure you click this short code block and this pre logic script code you import this string library and then under test one you have you can make the test one equal to string dot cap words and within brackets i'm going to specify the column which has the country iso country and i'm going to pass on empty space and click ok now you can see that we sort of converted this all caps united kingdom into something that looks like this so that's about it for text based operations using field calculator obviously you can explore it more and in the next video you will be able to see how to perform a couple of value based operations using python scripts in our GIS field calculated thank you
Info
Channel: GeoDelta Labs
Views: 4,480
Rating: 5 out of 5
Keywords:
Id: UL8MDZFhRxk
Channel Id: undefined
Length: 13min 5sec (785 seconds)
Published: Mon Mar 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.