1

whileループで形状ファイルのポイントを取得してJsonオブジェクトを作成したい.形状ファイルには73個のポイントが含まれています.形状ファイルのポイントをjsonオブジェクトに保存し、オブジェクトをループ内のArrayListに追加しています.whileループの下で取得していますオブジェクトを ArrayList から 1 つずつ取得して JsonArray に格納しますが、ここでの問題は、シェープファイルの最後のレコード ポイントのみが arraylist に 73 回格納されていることです。この問題を解決する方法を教えてください。

これが私のコードです

try {

        File file= new File(Environment.getDataDirectory().getAbsolutePath()+"/ne_50m_admin_0_tiny_countries.shp");
        Long fileLength=file.length();
        Log.d("FileLength is","file"+fileLength);
        String shpFile = Environment.getDataDirectory().getAbsolutePath().toString()+"/ne_50m_admin_0_tiny_countries.shp";
        //String shpFile = Environment.getDataDirectory().getAbsolutePath().toString()+"/ne_50m_admin_0_breakaway_disputed_areas.shp";
        ShapeReader reader = new ShapeReader(shpFile, true);
        ShapeType shpType=reader.getHeader().getShapeType();
        JSONObject jObject = new JSONObject();
        JSONObject wkidJson = new JSONObject();
        jObject.put("objectIdFieldName","OBJECTID");
        jObject.put("globalIdFieldName","");
        jObject.put("geometryType",shpType);
        wkidJson.put("wkid", new Integer(102100));
        jObject.put("spatialReference",wkidJson);
        JSONArray fieldsList = new JSONArray();
        JSONObject idJson = new JSONObject();
        JSONObject yJson = new JSONObject();
        JSONObject xJson = new JSONObject();
        idJson.put( "name","OBJECTID");
        idJson.put("alias", "OBJECTID");
        idJson.put("type", "esriFieldTypeOID");
        yJson.put( "name","y");
        yJson.put("alias", "y");
        yJson.put("type", "esriFieldTypeDouble");
        xJson.put( "name","x");
        xJson.put("alias", "x");
        xJson.put("type", "esriFieldTypeDouble");
        fieldsList.put(idJson);
        fieldsList.put(yJson);
        fieldsList.put(xJson);
        jObject.put("fields",fieldsList);
        JSONObject geomJson = new JSONObject();
        JSONObject attJson = new JSONObject();
        JSONObject featuresJson = new JSONObject();
        JSONArray featuresList = new JSONArray();
        Log.d("Header info is","Header"+reader.getHeader().getFileLength());
        recordshape=reader.new Record();
        ArrayList a= new ArrayList();
        while (reader.hasNext())
        {

            if(recordshape.end < fileLength)
            {
                if(shpType==ShapeType.POINT)
                {
                    recordshape=reader.nextRecord();
                    int recordNumber=recordshape.number;
                    double Xcoordinate= recordshape.minX;
                    double Ycoordinate=recordshape.minY;
                    Log.d("Xcoordinate info is","Xcoordinate"+Xcoordinate);
                    Log.d("Ycoordinate info is","Ycoordinate"+Ycoordinate);
                    geomJson.put("x", Xcoordinate);
                    geomJson.put("y", Ycoordinate);
                    attJson.put("OBJECTID", recordNumber);
                    attJson.put("y", Ycoordinate);
                    attJson.put("x", Xcoordinate);
                }

            }
            else
                break;

            featuresJson.put("geometry", geomJson);
            featuresJson.put("attributes", attJson);
            a.add(featuresJson);
            Log.d("featuresJson info is","featuresJson"+featuresJson);
         }
        Log.d("featuresJson info is","featuresJson"+featuresJson);
        Log.d("size info is","size"+a.size());
        for(int i=0;i<a.size();i++)
        {
        featuresList.put(a.get(i));
        }

        Log.d("featuresList info is","featuresList"+featuresList);
        jObject.put("features",featuresList);
        String jsonString = jObject.toString();

        jsonString = jsonString.replace("\\" , "");

        FileWriter writeFile = new FileWriter(Environment.getDataDirectory().getAbsolutePath().toString()+"/Test.json");
        BufferedWriter out = new BufferedWriter(writeFile);
        out.write(jsonString);
        out.flush();
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("Error","Message"+e.getMessage());
    }catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
            Log.d("Exception","Message"+e.getMessage());
    }

私が欲しいjsonのフォーマットは

{
"objectIdFieldName": "OBJECTID",
"globalIdFieldName": "",
"geometryType": "esriGeometryPoint",
"spatialReference": {
    "wkid": 102100
},
"fields": [
    {
        "name": "OBJECTID",
        "alias": "OBJECTID",
        "type": "esriFieldTypeOID"
    },
    {
        "name": "y",
        "alias": "y",
        "type": "esriFieldTypeDouble"
    },
    {
        "name": "x",
        "alias": "x",
        "type": "esriFieldTypeDouble"
    }
],
"features": [
    {
        "geometry": {
            "x": -13021472.272599999,
            "y": 4046325.7190999985
        },
        "attributes": {
            "OBJECTID": 212,
            "y": 4046325.7191,
            "x": -13021472.2726
        }
    },
    {
        "geometry": {
            "x": -13021124.9134,
            "y": 4046747.3976000026
        },
        "attributes": {
            "OBJECTID": 232,
            "y": 4046747.3976,
            "x": -13021124.9134

        }
    }
]

}

私を助けてくださいありがとう。

4

1 に答える 1

1

ループでは、同じ json オブジェクトに値を何度も入れます。したがって、それをjsonifyすると、配列にはn *同じオブジェクトが含まれます。

ループの各パスに追加する新しいオブジェクトを作成する必要があります。

これら 3 つの宣言は、ループ内で移動する必要があります。

JSONObject geomJson = new JSONObject();
JSONObject attJson = new JSONObject();
JSONObject featuresJson = new JSONObject();
于 2012-09-27T07:49:14.400 に答える