# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# CreateSample.py
# Created on: 2017-10-13 14:46:45.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

Web_Mercator = "PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID[" \
               "'WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]," \
               "PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing'," \
               "0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER[" \
               "'Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]] "
WGS_1984 = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]]," \
           "PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]] "
green_way_file_path = 'E:/武汉绿道/'
sample_json_path = 'E:/武汉绿道/Json/'
green_way_name = ['东湖绿道.shp',
                  '后官湖绿道.shp',
                  '江夏环山绿道.shp',
                  '金银湖绿道.shp',
                  '墨水湖绿道.shp',
                  '沙湖绿道.shp',
                  '月湖知音湖.shp',
                  '张公堤绿道.shp']
green_way_gdb = 'E:/武汉绿道/LVDAO.gdb'
tmp_buffer = 'tmpBuffer'
tmp_sample = 'tmpSample'
green_way_sample = 'LvDaoSample'
# Set Geoprocessing environments

arcpy.env.workspace = green_way_gdb
arcpy.env.overwriteOutput = True

for cur_name in green_way_name:
    arcpy.env.outputCoordinateSystem = Web_Mercator
    in_buffer_file = green_way_file_path + cur_name
    out_sample_json = sample_json_path + cur_name[0:-4] + '.json'
    arcpy.Buffer_analysis(in_buffer_file, tmp_buffer, "500 Meters", "FULL", "ROUND", "ALL", "", "PLANAR")

    for row in arcpy.da.SearchCursor(tmp_buffer, ["SHAPE@"]):
        ext = row[0].extent
        origin_coord = arcpy.Point(ext.XMin, ext.YMin)
        origin_coord = str(ext.XMin) + ' ' + str(ext.YMin)
        corner_coord = arcpy.Point(ext.XMax, ext.YMax)
        corner_coord = str(ext.XMax) + ' ' + str(ext.YMax)
        y_axis_coord = arcpy.Point(ext.XMin, ext.YMax)
        y_axis_coord = str(ext.XMin) + ' ' + str(ext.YMax)
        arcpy.CreateFishnet_management(tmp_sample, origin_coord, y_axis_coord, "200", "200", "", "", corner_coord,
                                       "LABELS", tmp_buffer, "POLYLINE")

    arcpy.env.outputCoordinateSystem = WGS_1984
    intersect_features = [tmp_sample + '_label', tmp_buffer]
    arcpy.Intersect_analysis(intersect_features, green_way_sample)
    arcpy.AddXY_management(green_way_sample)
    fieldName = 'GREENWAY_NAME'
    arcpy.AddField_management(green_way_sample, fieldName, "TEXT")
    expression = "'" + cur_name[0:-4] + "'"
    arcpy.CalculateField_management(green_way_sample, fieldName, expression, "PYTHON_9.3")

    arcpy.FeaturesToJSON_conversion(green_way_sample, out_sample_json, "FORMATTED", "NO_Z_VALUES", "NO_M_VALUES")
