Tuesday, 20 September 2016

python - Create new polygon layers from postcodes latitude & longitude


First at all, I want to say that I am geocoding enthusiastic but have no formal background on GIS. I'll do my best to use the right wording and make myself clear what I am striving for.


Also the only tool I have available for this task is Python. This is a project I am working on in my own time but hopefully will kick off at work if I manage to produce something meaningful.


I have downloaded the Code-Point® Open from the Ordnance Survey that contains postcode units (~1.7M) for Great Britain and converted eastings & northings to Lat & Lon in WGS84.


From postcodes I've also created sector, district & area levels and a custom level that regroups areas into North, South, East & West. So my final product is a csv file that contains the levels mentioned above plus Lat & Lon. The granularity of my file is postcode level


What I want to do is to create a layer for each level and then use them later on TileMill or google map in order to create a thematic map where each patch/polygon will change color based on specific events.



In my view, what I need to do is to group these coordinates based on each level, create a collection of polygons and then create or export them as a layer (or shapefile??).


I've been looking to Shapely but I am not sure whether is the right tool for it and online examples don't make much of a sense to me due to my lack of knowledge on this matter.


Can anyone please point me out on the right direction or provide me with a small piece of code as example?


Any advice will be highly appreciated!


[EDIT 1]


I created my layer as per the below instructions but can't see my area polygons yet.


enter image description here


[EDIT 2]


I've all over again 3 times and still don't get to see the polygons - I've checked my csv file and the area column has values as expected (120 areas). Here it is another screenshot with layer metadata.


enter image description here



And here it is a screenshot with all layers apart from original - it shows nothing.


enter image description here


[Edit 3]


Query option disabled.


enter image description here


[Edit 4] After 4hrs processing I get to see my polygons:


In summary these are the steps:



  1. Import csv file

  2. Export file as shapefile


  3. Re-import shapefile (could be new on a new project)

  4. Apply the query logic

  5. Apply the Convex Hull and wait.

  6. that's pretty much


enter image description here



Answer



With your complete CSV file in hand and QGIS installed on your laptop this is what you need to do. Sorry if this seems like much if you haven't used QGIS before, but use it and learn it; it will be your best friend in this stuff.


You want to use Add Delimited Text Layer. If you cannot find it on your plugins toolbar you might need to activate/install from Plugins > Manage Plugins or Plugins > Fetch Python Plugins. Start import row as needed to avoid titles and finally set XY Fields to their appropriate columns. If it is every Postal Code in GB then it might take a few seconds to render unless you have a powerful laptop; in this case I would turn off the render engine but since you are a novice, seeing what you are doing helps you understand the process.


enter image description here



This should result in a layer with a bunch of point data, and if you go to the Attribute Table you should see all of your extra fields as well.


Now go to Vector > Geoprocessing Tools > Convex hulls. Select your point layer as input. Select Create convex hulls based on input field, select the field upon your polygons will be created and select a save file name for the new layer. The result should look similar to below.


enter image description here


Now you have your polygons based upon your nodes and your selected column. You just repeat for each column value and create layers until you have all your desired polygon. In this example I created the polys based upon a field in the point layer where I assigned groups of points values 1 through 3.


Here you can see some issues you might need to remedy.


First, all outside points define the edge of the poly. It may look odd but if you are just using the poly as an aggregation symbol it shouldn't be a problem. You can remedy this by apply Vector > Geoprocessing Tools > Buffers first and then using the resulting layer to build the hulls off of.


Secondly, you might have some overlaps which we will fix with hand digitizing, and thirdly we have some gaps (which may not be an issue for you) which we will fixing with digitizing as well.


Select your layer and click the edit layer button. Go to Edit > Snapping Options. Activate snapping on your layer, to vertex, and set the tolerance to a setting that seems right for your work. You may have to change a few times, depends on your zoom level and resolution for your work.


Now select nodes of the polygon and move them to suitable locations to avoid overlap and gaps. Use the snapping function to create seamless, juxtaposed polygons. I may want to turn off your point layer to make it easier to see the poly nodes. You can double click a polygon's border to create new nodes if you need them. Click your Save Edit to save your work occasionally and then clicked the Edit Layer to finalize your work.


The de-facto format for layers is the Shapefile. but you can right click and save Save As... your file to many formats. Save your layers to whatever is best for your needs.



Learn to use QGIS it will make quick work of such things and won't require (most of the time, ha) you to create complex workflows to complete tasks.


A word about online mapping; It sounds like you are doing what did, starting from a budget of nothing. Google Maps API's term-of-service don't allow closed network use unless you have a corporate license, which cost money. OpenLayers and LeafletJS are free map APIs but don't have the map tiles built-in like Google's. But there are many tile service out there, most which work with OpenLayers and LeafletJS, but look and see which will fit your needs the best. You can visit Open Street Map's website for information about some free services. If you have a medium-sized company that isn't passing out your information like candy, usage will be low and you can safely use these without making a system admin mad.


In any case you will need to pickup some knowledge of Javascript, HTML5, web development, etc to effectively create online maps if you don't know this already.


If you are not comfortable with this just quite yet look at using QGIS' layout designer and just create some printed graphics with your data. The layout designer is a little primitive to say that of InDesign or ArcGIS but it still very nice. If the data doesn't need to be bleeding-edge updated, say once a month, this might work for now.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...