# Urban housing selection analysis (ArcPy Implementation)

Keywords: Python arcgis Arcpy

### 1. Background

How to find a residential area with good environment, convenient shopping and convenient school for children is the most concerned problem of property buyers. Therefore, buyers need to study and analyze the information of commercial housing as a whole and choose the most suitable purchase location.

### 2. Purpose

Learn to use buffer analysis and overlay analysis to solve practical problems.

### 3. Data

The test data is located in "\ Chp7\Ex_1

### 4. Requirements

The urban area sought is low noise, close to the commercial center and famous high schools, for the sake of elegant environment and close to places of interest. Based on the above conditions, a quantitative limit is given as follows:
(1) 200m away from the main urban traffic artery, the traffic flow of the traffic artery is large, which is the main source of noise (ST is the main urban traffic artery in the road type);
(2) For the impact from the large commercial center, the impact area is determined by the size of the commercial center, specifically by its attribute field YUZHI;
(3) It is within 750m from the famous high school, so that children can go to school conveniently;
(4) Within 500m from scenic spots and historic sites, the environment is elegant;

The specific flow chart is as follows:

### 6. ArcPy implementation

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# 7-1 urban housing selection analysis.py
# Created on: 2021-10-09 20:55:29.00000
#   (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
import os

path = raw_input("Please enter the corresponding absolute path of the required data:").decode("utf-8")
paths = path + '\\result'
if not os.path.exists(paths):
os.mkdir(paths)

# Local variables:
network = path + "\\network.shp"
network_Select_shp = "network_Select.shp"
network_Select_Buffer200_shp = "network_Select_Buffer200.shp"

Marketplace = path + "\\Marketplace.shp"
Marketplace_Buffer_shp = "Marketplace_Buffer.shp"

school = path + "\\school.shp"
school_Buffer_shp = "school_Buffer.shp"

famous_place = path + "\\famous place.shp"
famousplace_Buffer_shp = "famousplace_Buffer.shp"

suitable_shp = u"Regional residential suitability classification.shp"

# Set Geoprocessing environments
arcpy.env.scratchWorkspace = paths
arcpy.env.workspace = paths

# Process: filtering
print "print Process: screen"
arcpy.Select_analysis(network, network_Select_shp, "\"TYPE\" = 'ST'")

# Process: buffer
print "Process: buffer"
arcpy.Buffer_analysis(network_Select_shp, network_Select_Buffer200_shp, "200 Meters", "FULL", "ROUND", "ALL", "", "PLANAR")

arcpy.AddField_management(network_Select_Buffer200_shp, "voice", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: calculated fields
print "Process: Calculation field"
arcpy.CalculateField_management(network_Select_Buffer200_shp, "voice", "-1", "VB", "")

# Process: buffer (2)
print "Process: buffer (2)"
arcpy.Buffer_analysis(Marketplace, Marketplace_Buffer_shp, "YUZHI_", "FULL", "ROUND", "ALL", "", "PLANAR")

arcpy.AddField_management(Marketplace_Buffer_shp, "market", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: calculated field (2)
print "Process: Calculation field (2)"
arcpy.CalculateField_management(Marketplace_Buffer_shp, "market", "1", "VB", "")

# Process: buffer (3)
print "Process: buffer (3)"
arcpy.Buffer_analysis(school, school_Buffer_shp, "750 Meters", "FULL", "ROUND", "ALL", "", "PLANAR")

arcpy.AddField_management(school_Buffer_shp, "school", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: calculated field (3)
print "Process: Calculation field (3)"
arcpy.CalculateField_management(school_Buffer_shp, "school", "1", "VB", "")

# Process: buffer (4)
print "Process: buffer (4)"
arcpy.Buffer_analysis(famous_place, famousplace_Buffer_shp, "500 Meters", "FULL", "ROUND", "ALL", "", "PLANAR")

arcpy.AddField_management(famousplace_Buffer_shp, "famous", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: calculated field (4)
print "Process: Calculation field (4)"
arcpy.CalculateField_management(famousplace_Buffer_shp, "famous", "1", "VB", "")

# Process: Federation
print "Process: union"
arcpy.Union_analysis(u"{}\\network_Select_Buffer200.shp #;{}\\Marketplace_Buffer.shp #;{}\\school_Buffer.shp #;{}\\famousplace_Buffer.shp #".format(paths,paths,paths,paths), suitable_shp, "ALL", "", "GAPS")

# Process: deleting fields
print "Process: Delete field"

arcpy.AddField_management(suitable_shp, "class", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: calculated field (5)
print "Process: Calculation field (5)"
arcpy.CalculateField_management(suitable_shp, "class", "[voice] + [market] + [famous] + [school]", "VB", "")

ListFeatureClasses = arcpy.ListFeatureClasses()  # Count the number of feature classes
for Feature in ListFeatureClasses:  # Loop through and delete unnecessary feature classes
if u"suitable" not in Feature:
print "Deleting{}".format(Feature)
arcpy.Delete_management(Feature)

print "Operation completed~~~"

Note: if there is an error in deleting a field, it may be a problem of garbled code. Simply delete the code of deleting a field and continue to execute it,
Deleting fields is just simplifying the property sheet!

### 7. Result display

Later, the final results are divided into five grades. The more green, the higher the score, the better the suitability. The more red, the lower the score, the worse the suitability. The regional residential suitability grading is completed.

Continue with the next experiment O(∩ ∩) O~~~

Posted by 1josh13 on Thu, 14 Oct 2021 13:48:59 -0700